【问题标题】:How to read file line by line and print to a text box c#如何逐行读取文件并打印到文本框c#
【发布时间】:2019-01-05 01:38:09
【问题描述】:

我正在开发一个 Windows 窗体应用程序,我想从我的本地计算机中获取一个文本文件,让应用程序读取文本文件并将文件中的每一行文本显示到应用程序的文本框中。我想在表单上按下一个按钮并显示文本文件的第一行,然后再次按下该按钮并显示第二行等。我一直在寻找这样做的方法,并发现 StreamReader 会可能最适合我想要实现的目标。

我目前有以下代码,但它似乎将每一行打印到一行。如果有人能看出原因,将不胜感激,我确信它是小东西。

private void btnOpen_Click(object sender, EventArgs e)
{
    string file_name = "G:\\project\\testFile.txt";
    string textLine = "";

    if (System.IO.File.Exists(file_name) == true)
    {
        System.IO.StreamReader objReader;
        objReader = new System.IO.StreamReader(file_name);

        do
        {
            textLine = textLine + objReader.ReadLine() + "\r\n";
        } while (objReader.Peek() != -1);

        objReader.Close();
    }
    else
    {
        MessageBox.Show("No such file " + file_name);
    }

    textBox1.Text = textLine;
}

【问题讨论】:

  • 如果你想为每个 btn 点击读取一行,你不能使用 while 循环来读取整个文件。您只能在每次单击时执行 .ReadLine()。其次,你必须使用多行文本框。
  • 在整个过程中没有打开流。您应该将所有行放入一个字段中,然后遍历它们。如果您有非常大的文件,您可以尝试通过按钮按下事件记住流滚动。
  • @ElConrado 您可以读取整个文件,将每一行存储在一个变量中(可能是 List),然后只需使用索引器并以这种方式添加一个新行?

标签: c# windows-forms-designer streamreader


【解决方案1】:

我会按照以下方式进行:

您正在使用 Windows 窗体,因此您有一个 Form 类作为您的主类。

在这个类中我会定义:

private string[] _fileLines;
private string _pathFile;
private int _index = 0;

在构造函数中我会这样做

_fileLines = File.ReadAllLines(_pathFile);

在按钮单击事件处理程序中我会这样做:

textBox1.Text = _fileLines[_index++];

【讨论】:

    【解决方案2】:

    给定

    private string[] lines;
    private int index =0;
    

    点击事件

    // fancy way of intializing the lines array
    lines = lines ?? File.ReadAllLines("somePath");
    
    // sanity check 
    if(index < lines.Length)
       TextBox.Text = lines[index++]; // index++ increments after use
    

    其他资源

    File.ReadAllLines Method

    打开一个文本文件,将文件的所有行读入一个字符串数组, 然后关闭文件。

    ?? Operator (C# Reference)

    ??运算符称为空合并运算符。它返回 如果操作数不为空,则为左操作数;否则返回 右手操作数。

    ++ Operator (C# Reference)

    一元递增运算符 ++ 将其操作数递增 1。它是 支持两种形式:后缀自增运算符、x++ 和 前缀自增运算符,++x。

    更新

    如果我要不断地用新行更新文本文件,我 想通过单击按钮逐行阅读,我该怎么做 去吗?

    你可以只对行使用局部变量,每次都读取文件

    var lines = File.ReadAllLines("somePath");
    if(index < lines.Length)
       TextBox.Text = lines[index++];
    

    【讨论】:

    • Perfect 非常感谢,如果我要不断地用新行更新文本文件,并且我想通过单击按钮逐行阅读,我该怎么做?这个解决方案会在程序开始时读取所有行时不起作用吗?我可以循环检查线路更新吗?
    【解决方案3】:

    你可以这样逐行读取文本文件:

    public int buttonClickCounter = 0;
    private void button1_Click_1(object sender, EventArgs e)
    {   
       List<string> fileContentList = new List<string>();
       string fileInfo = "";
       StreamReader reader = new StreamReader("C://Users//Rehan Shah//Desktop//Text1.txt");
       while ((fileInfo = reader.ReadLine()) != null)
       {
          fileContentList.Add(fileInfo);
       }
    
       try
       {
          listBox1.Items.Add(fileContentList[buttonClickCounter]);
          buttonClickCounter++;
       }
       catch (Exception ex)
       {
          MessageBox.Show("All Contents is added to the file.");
       }
    }
    

    【讨论】:

      【解决方案4】:

      textLine = textLine + objReader.ReadLine() + "\r\n";

      用下面的代码替换

      textLine = textLine + objReader.ReadLine() + Environment.NewLine;

      【讨论】:

      • Environment.NewLine 只是一个包含"\r\n""\n"static property,具体取决于平台是否为Unix。我同意拥有它比拥有它比"\r\n" 更好,但这并不能解决他的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多