c# - Putting textfile into Listbox using streamreader -
i wondering how can make program read text file , put contents in listbox using streamreader?
private void button1_click(object sender, eventargs e) { } (streamreader stread = new streamreader("c:\users\tommy\desktop\windowsformsapplication9\windowsformsapplication9\bin\debug\transactions.txt")) { while (!stread.endofstream) { listbox1.items.add(stread.readline()); }
using file.readalllines
be aware of @ @ beginning of file path. using backslash in string literal must escaped if @ not used.
listbox1.items.addrange(file.readalllines(@"c:\users\tommy\desktop\windowsformsapplication9\windowsformsapplication9\bin\debug\transactions.txt"));
// create instance of streamreader read file. // using statement closes streamreader. using (streamreader sr = new streamreader("testfile.txt")) { string line; // read , display lines file until end of // file reached. while ((line = sr.readline()) != null) { listbox1.items.add(line); } }
Comments
Post a Comment