c#的openFileDialog的简单实例

来源:百度知道 编辑:UC知道 时间:2024/06/07 10:54:37
初学csharp 在那文件的openFiledialog看看如下代码是哪儿错了
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.InitialDirectory = "D:\\";
open.Filter = "文本文件|*.*|C#文件|*.CS|所有文件|*.*";
open.RestoreDirectory = true;
open.FilterIndex = 1;
if (open .ShowDialog ()==DialogResult .OK )
{
string fname = open.FileName;
StringReader sr = FileDialog.OpenText(fname);
string str;
while ((str.ReadLine())!=null)
{
this.richTextBox1 .txt+=str ;
}

}
}提示为错误找不到类型或命名空间名称“StringReader”(是否缺少 using 指令或程序集引用?)我是照着书上来写的 不晓得哪儿出问题了 我在输入stringreader的时候也没有出现那些提示下框 另外还能解释一下那while那段代码吗?

缺少引用
最顶上using的地方要加
using System.IO;

while的地方意思是每次读取一行,直到文件末,并将读取的内容追加到richtextbox控件的最后

读取的while部分应该是错了
string str;
while ((str = sr.ReadLine())!=null)
{
this.richTextBox1 .txt+=str ;
}

StringReader的命名空间在 using System.IO;
需要引用一下

而且 你的OpenText ReadLine 都是什么东西呀!

using System.IO;
加在最上面一排using的地方

while是说当读取的行不为null(空)的时候,执行循环。
也就是说,一直读到末尾,把每个行都读出来 然后累加到richTextBox1 中

因为你没有包含System.IO命名空间,在最上面写:using System.IO;
或者把StringReader写成System.IO.StreamReader。
while里面的内容str.ReadLine())!=null就是把str里面的内容一行一行的读取出来,一直到最后一行。
this.richTextBox1 .txt+=str ;每读一行则把内容添加到richTextBox1里面。