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..
Asked
Active
Viewed 1.6k times
2 Answers
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.

markymark_prod
- 17
- 4

Darren
- 68,902
- 24
- 138
- 144
-
I am so stupid, I already used the ReadLine() command but forgot to save the line as the text of the label LOL but thank you and the other guys! – iAmFastAndYou Jun 08 '13 at 14:00
-
@iAmFastAndYou - glad you got it sort. If the answers helped please mark one as the answer. – Darren Jun 08 '13 at 14:01
-
Do you know how to get back to the first line of the document? – iAmFastAndYou Jun 08 '13 at 14:03
-
1@iAmFastAndYou - http://stackoverflow.com/questions/2053206/return-streamreader-to-beginning – Darren Jun 08 '13 at 14:05
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