不知道你那个RichTextBoxText什么意思,如果你用RichTextBox的话,他有OpenFile方法,可以直接打开文件
C#里有openFileDialog控件的额,刚我们老师正好给我们讲了一个额,作为参考:
this.openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
this.openFileDialog1.Filter = "文本文档(*.txt)|*.txt";
this.openFileDialog1.FileName = "*.txt";
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
path = this.openFileDialog1.FileName;
FileStream fs = null;
if ("".Equals(path))
{
fs = new FileStream(path, FileMode.OpenOrCreate);
}
else
{
fs = new FileStream(path, FileMode.Open);
}
StreamReader sr = new StreamReader(fs, Encoding.Default);
this.richTextBox1.Text = sr.ReadToEnd();
sr.Close();
fs.Close();
}
else
{
MessageBox.Show("您未打开任何文档!");
}