c# streamreader 的readline方法有关问题
c# streamreader 的readline方法问题
d:\test.txt内容如下:
1
2
3
4
5
6
7
8
9
end
程序运行后只读到了2 4 6 8 end
为什么程序只读偶数行。。。
新手还望大牛不要嘲笑。

------解决方案--------------------
改成
------解决方案--------------------
你while循环中有一次sr.ReadLine()了,这样就又向后读取了一行
------解决方案--------------------
按照楼主原来的写法,在修改TextBox的值前已经读了一行了
private void button1_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("d:\\test.txt", FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs);
while (sr.ReadLine() != null)
{
textBox1.Text += sr.ReadLine();
}
}
d:\test.txt内容如下:
1
2
3
4
5
6
7
8
9
end
程序运行后只读到了2 4 6 8 end
为什么程序只读偶数行。。。
新手还望大牛不要嘲笑。
------解决方案--------------------
改成
string s="";
while (s=sr.ReadLine() != null)
{
textBox1.Text += s;
}
------解决方案--------------------
你while循环中有一次sr.ReadLine()了,这样就又向后读取了一行
------解决方案--------------------
按照楼主原来的写法,在修改TextBox的值前已经读了一行了