C#里做一个类似记事本的程序,打开文件的代码怎么实现?下面那个应该改成什么?

2024-11-28 23:45:57
推荐回答(2个)
回答(1):

不知道你那个RichTextBoxText什么意思,如果你用RichTextBox的话,他有OpenFile方法,可以直接打开文件

回答(2):

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("您未打开任何文档!");
}