qq_46149582 2020-07-01 03:22 采纳率: 100%
浏览 116
已采纳

c#读取文本,不能全部读出来?

private void button2_Click(object sender, EventArgs e)
{
string filePath = "";
OpenFileDialog opd = new OpenFileDialog();
if (opd.ShowDialog() == DialogResult.OK)
{
filePath = opd.FileName;
}
StreamReader sr = new StreamReader(filePath,Encoding.Default);
string s = sr.ReadLine();
while (sr.ReadLine()!=null)
{
textBox1.Text+=s + "\r\n";
s = sr.ReadLine();
}
}
创建一个10行的文本,为什么只能读出来奇数行?无法全部读出来,怎么解决?

  • 写回答

1条回答 默认 最新

  • wlj1234 2020-07-01 10:26
    关注

    while (sr.ReadLine()!=null) //这里又读了一行
    改如下
    string s = sr.ReadLine();
    while (s!=null)
    {
    textBox1.Text+=s + "\r\n";
    s = sr.ReadLine();
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?