OpenFileDialog openFileDialog1 = new OpenFileDialog();
this.openFileDialog1.InitialDirectory = "c:\\"; openFileDialog1.Filter = "txt files (*.txt)|*.txt" ;
openFileDialog1.FilterIndex = 1 ;
openFileDialog1.RestoreDirectory = true ; if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = this.openFileDialog1.FileName;
}

解决方案 »

  1.   

    snof(雪狼)  谢谢你,不过你说的这些我都知道,我想的是一些用于打开文件的代码!
      

  2.   

    打开.txt或者.rtf
    private void open_Click(object sender, System.EventArgs e)
    {
    string lstrfilename="";
    string lstrtemp="";
    if( openFileDialog1.ShowDialog()==DialogResult.OK)
    {
    lstrfilename=openFileDialog1.FileName;
    strdocumentname=lstrfilename;
    }
    else
    {
    return;
    }
    FileStream lfstream=null;
    string lstrfilecontents="";
    try
    {
    lstrtemp="";
    int liretval=1;
    lfstream=new FileStream(lstrfilename,FileMode.Open,FileAccess.Read);
    Byte[] lbyte=new Byte[1024];
    lbyte.Initialize();
    for (;liretval!=0;)
    {
    lbyte.Initialize();
    lstrtemp="";
    liretval=lfstream.Read(lbyte,0,1024);
    lstrtemp=Encoding.ASCII.GetString(lbyte,0,1024);
    lstrfilecontents+=lstrtemp;
    }
    }
    catch(FileNotFoundException el)
    {string lstrMessage="File not found or permission denied";
    MessageBox.Show(lstrMessage,"word pad",MessageBoxButtons.OK ,MessageBoxIcon.Error);
    return;
    }
    catch(Exception e2)
    { string lstrError="Error:"+e2;
    MessageBox.Show(lstrError,"word pad",MessageBoxButtons.OK ,MessageBoxIcon.Error);
    return;
    }
    finally
    {
    if (lfstream!=null)
      {lfstream.Close();
      }
    }
    lstrtemp=lstrfilename.ToLower();
    if(lstrtemp.EndsWith(".rtf"))
    {
    richTextBox1.Rtf=lstrfilecontents;
    itypeofdocument=2;
    }
    else
    { richTextBox1.Text=lstrfilecontents;
                  itypeofdocument=1;
    }
    this.Text="word pad"+lstrfilename;
    richTextBox1.Select(0,0);
                bsaveflag=false;//这个用来判断是否保存的
    }
      

  3.   

    可以捕捉OpenFileDialog.filename 最后“\"后面的字符串。。用SUBSTRING方法晕忘了。
      

  4.   

    取得文件全路径名后FileInfo info = new FileInfo(strFilePath);
    string strFileName = info.Name; //取得纯文件名FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
    byte[] bytes = new byte[fs.Length];
    fs.Read(bytes, 0, bytes.Length);
    fs.Close();这样就把文件读到了byte[]里
      

  5.   

    liyj(好好学习 天天向上) 你可不可以再加上一些注释和说明啊