private void openFileDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
//我想通过openFileDialog在rtfTex中显示打开的文件,参 数怎么弄?
this.rtfText.LoadFile();


}
catch(System.IO.FileNotFoundException)
{
MessageBox.Show("No file to load yet");
}
}

解决方案 »

  1.   

    private void openFileDialogBTN_Click(o b j e c t sender, System.EventArgs e){
    OpenFileDialog openFileDialog=new OpenFileDialog();
    openFileDialog.InitialDirectory="c:\\";//注意这里写路径时要用c:\\而不是c:\
    openFileDialog.Filter="文本文件|*.*|C#文件|*.cs|所有文件|*.*";
    openFileDialog.RestoreDirectory=true;
    openFileDialog.FilterIndex=1;
    if (openFileDialog.ShowDialog()==DialogResult.OK)
    {
    fName=openFileDialog.FileName;
    File fileOpen=new File(fName);
    isFileHaveName=true;
    richTextBox1.Text=fileOpen.ReadFile();
    richTextBox1.AppendText("");
    }
    }参考
    http://singlepine.cnblogs.com/articles/255772.html
      

  2.   

    OpenFileDialog cOpenFileDialog = new OpenFileDialog();
    cOpenFileDialog.Filter = "wav files (*.wav)|*.wav|mp3 files (*.mp3)|*.mp3";
    cOpenFileDialog.FilterIndex = 1;if (cOpenFileDialog.ShowDialog() == DialogResult.OK)
    {
        if (cOpenFileDialog.FileName == null || cOpenFileDialog.FileName == "")
        {
    MessageBox.Show("Please select a file.");
    return;
        }
        if (File.Exists(cOpenFileDialog.FileName) == false)
        {
    MessageBox.Show("The picture file is not exist.");
    return;
        }
        if (LoadVoiceFile(cOpenFileDialog.FileName) != SYS_RETURN.OK)
    MessageBox.Show("Fail to open picture file.");
    }
      

  3.   

    OpenFileDialog.FileName 获取文件路径File。Open打开文件到流:::::FileStream fs ; fs = File.Open(OpenFileDialog.FileName, FileMode.Open, FileAccess.Read);byte[] b = new byte[1024];
        UTF8Encoding temp = new UTF8Encoding(true);   while (fs.Read(b,0,b.Length) > 0) 
         {
         Console.WriteLine(temp.GetString(b));
         }
      

  4.   

    上面的代码已经相当详细了。 ShowDialog 就是打开窗口的函数啊。什么时间需要打开窗口,调用它就行了
      

  5.   

    为什么我用this.rtfText.LoadFile(openFileDialog.FileName);会产生异常
      

  6.   

    那得看是什么异常了。如果是 FileName 没设置到实例,那肯定是用户没有点 OK 按钮,或者你根本就没 ShowDialog 过。不过通常来说不会,因为我记得 FileName 有默认值。再就可能是 LoadFile 的异常。我以前似乎用一个参数的时候也出过错。后来用两个参数的重载方法就好了。第二个参数是 RichTextBoxStreamType 枚举。PlainText: A plain text stream that includes spaces in places of Object Linking and Embedding (OLE) objects.  RichNoOleObjs: A Rich Text Format (RTF) stream with spaces in place of OLE objects. This value is only valid for use with the SaveFile method of the RichTextBox control.  RichText: A Rich Text Format (RTF) stream.  TextTextOleObjs: A plain text stream with a textual representation of OLE objects. This value is only valid for use with the SaveFile method of the RichTextBox control.
      
    UnicodePlainText: A text stream that contains spaces in place of Object Linking and Embedding (OLE) objects. The text is encoded in Unicode.  这是 MSDN 上对这个枚举各个成员的解释。你根据你的文件的编码及格式选择正确的参数就行了。
      

  7.   

    private void button1_Click_1(object sender, System.EventArgs e)
    {
    if(this.openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    System.IO.StreamReader sr = new System.IO.StreamReader(this.openFileDialog1.FileName,System.Text.Encoding.GetEncoding("GB2312"));
    this.textBox1.Text = sr.ReadToEnd();
    r.Close();
    }
    }