加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#ReadLine不读取整个文件

发布时间:2020-12-16 10:31:19 所属栏目:百科 来源:网络整理
导读:我使用此代码从我的文件中读取每一行,但它只读取(或只显示文件的最后一行).当我在记事本中打开文件时,我可以看到有多行. 这是我正在使用的代码: using (StreamReader sr = File.OpenText(newPath)) { String input; while ((input = sr.ReadLine()) != null
我使用此代码从我的文件中读取每一行,但它只读取(或只显示文件的最后一行).当我在记事本中打开文件时,我可以看到有多行.
这是我正在使用的代码:

using (StreamReader sr = File.OpenText(newPath))
        {
            String input;
            while ((input = sr.ReadLine()) != null)
            {

                TextBox1.Text = input;  

            }

解决方法

你需要使用:

TextBox1.Text += input;

否则描述的行为是正确的

最终你可以改变你的样本:

String input;
String target = String.Empty;
try {
using (StreamReader sr = File.OpenText(newPath)) 
{ 
    while ((input = sr.ReadLine()) != null) 
    { 
        target += input;   
    }
}
TextBox1.Text = target;
} catch { ... }

最好的方法是将阅读过程提取到一个单独的方法中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读