Reputation: 1
Can I read the previous line using StreamReader?
Dim previousfile As New StreamReader("file.txt")
If previousfile.Peek <> +1 Then
txtName.text = previousfile.ReadLine
End If
Can anyone help?
Upvotes: 0
Views: 863
Reputation: 34218
You can't read backwards with a StreamReader
, but if you read all of the lines in first you can traverse them however you like. This does mean reading the whole file in up front, which may be less efficient depending on your usage, but this method would do the job and give you an array:
var lines = File.ReadAllLines("file.txt")
Upvotes: 0
Reputation: 5822
you cannot read the previous line - StreamReader really is a forward only type of reader. when you read a line... thats it. you cannot go back. why dont you hold the previous line being read in a temp variable or maybe use the FileStream which has a Seek method which maybe of some use to you?
or why not read the entire contents into a collection of strings and splitting it on some delimeter for example?
Upvotes: 0