日期:2014-05-17  浏览次数:21208 次

如何在VS中,用C#显示文件打开窗口??
可能我问题说的不是很清楚,因为我自己不知道怎么描述,嘿嘿...
就是VS里面,我用的是C#编辑语言。我现在就是想能有个代码,运行后可以弹出个打开文件的那种窗口,然后我还需要的是可以判断打开的是以什么结尾的文件,文件里面的内容,如果在代码中把读取的内容取出来!
我只知道在C#中的文件读些是用FileStream,和个叫做什么Read的类吧...
请哪些告诉详细点告诉我,谢谢了!


------解决方案--------------------
打开文件对话框
C# code

protected void button1_Click(object sender, System.EventArgs e)
{ 
   OpenFileDialog openFileDialog1 = new OpenFileDialog();
   openFileDialog1.InitialDirectory = "c:\\" ;
   openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;//设置打开文件类型
   openFileDialog1.FilterIndex = 2 ;
   openFileDialog1.RestoreDirectory = true ;
   if(openFileDialog1.ShowDialog() == DialogResult.OK)
   {
      if(openFileDialog1.FileName!= "")
      {
        MessageBox.Show("你选择了"+openFileDialog1.FileName);//得到文件路径
      }
   }
}

------解决方案--------------------
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开(Open)";
ofd.FileName = "";
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments
ofd.Filter = "文本文件(*.txt)|*.txt";
ofd.ValidateNames = true;
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
try
{
if (ofd.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(ofd.FileName, System.Text.Encoding.Default);
this.richTextBox1.Text = sr.ReadToEnd();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}

------解决方案--------------------

OpenFileDialog dlg = new OpenFileDialog();
dlg .Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if(dlg.ShowDialog() == DialogResult.OK)
{
if(dlg.FileName!= "")
{
MessageBox.Show("文件名:"+dlg.FileName + "\n扩展名:" + Path.GetExtension(dlg.FileName);
}
}


------解决方案--------------------
C# code
string path = "controlKeyboard.exe";   //将文件存放在bin目录下
//string path = "D:\\controlKeyboard.exe";  //调用非bin目录下的程序路径
ProcessStartInfo startinfo = new ProcessStartInfo(path);
startinfo.WindowStyle = ProcessWindowStyle.Normal;
Process.Start(startinfo);