C#里做一个类似记事本的程序,打开文件的代码怎么实现?下面那个应该改成什么?
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("您未打谈搏仿开任何文档银毁!");
}
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("您未打谈搏仿开任何文档银毁!");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询