简体   繁体   中英

How to browse a text file & add items to listbox c#/.net

I want to add items to a "listbox" from a text file by browsing the file that contains text like this:

"one"

"two"

"three"

"many more"

Please tell me how to browse a text file & add items to a listbox.

foreach (var line in System.IO.File.ReadAllLines("file.txt"))
{
   listbox.Items.Add(line);
}

The code below reads a file and places the text line by line into the Listbox

   ListBox lb = new ListBox();
    System.IO.StreamReader sr = new System.IO.StreamReader("Path to File");

    while (!sr.EndOfStream)
    {
        lb.Items.Add(sr.ReadLine());
    }

    sr.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM