How to add lines of a text file into individual items on a ListBox (C#)

前端 未结 4 1891
渐次进展
渐次进展 2021-01-23 00:52

How would it be possible to read a text file with several lines, and then to put each line in the text file on a separate row in a ListBox?

The code I have so far:

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-23 00:56

    You should use a streamreader to read the file one line at a time.

    using (StreamReader sr = new StreamReader("ignore.txt"))
    {
      string line;
      while ((line = sr.ReadLine()) != null)
        listBox1.Items.Add(line);
    }
    

    StreamReader info -> http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx

    ListBox info -> http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.aspx

提交回复
热议问题