1

I want to know how to read the next line of a text document. If I click the button, it should open the text document and read the first line. Then, if I click the "next" button it should read the next line. How can I do this second button? In C and other languages there are some commands for this..

svick
  • 236,525
  • 50
  • 385
  • 514
iAmFastAndYou
  • 63
  • 1
  • 1
  • 6

2 Answers2

5

You need a StreamReader object and then you can invoke the ReadLine method. Don't forget to add the "@" symbol before file path name.

StreamReader sr = new StreamReader(@"C:\\YourPath.txt");

Then on your button clicks you can do:

var nextLine = sr.ReadLine();

The result of each line will be stored in the nextLine variable.

Darren
  • 68,902
  • 24
  • 138
  • 144
4

You can use StreamReader.ReadLine

if (myStreamReader.Peek() >= 0) 
   {
      string line = myStreamReader.ReadLine();
   }

If you don't want to keep the file open, you can start by reading all lines into memory using File.ReadAllLines

string[] allLines = File.ReadAllLines(path);
lightbricko
  • 2,649
  • 15
  • 21