iAmFastAndYou
iAmFastAndYou

Reputation: 63

How to read the next line in a text document?

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..

Upvotes: 1

Views: 16983

Answers (2)

Darren
Darren

Reputation: 70728

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.

Upvotes: 5

lightbricko
lightbricko

Reputation: 2709

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);

Upvotes: 4

Related Questions