NEWBIE - Determine or Set location in a StreamReader

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

Greetings,

If I want to determine the line number in the text file that I am currently
reading (In a StreamReader.ReadLine process), how can I do that?

If I want to start a StreamReader.ReadLine process at a certain line number
in a text file, how can I do that?

Thanks,

-Dave
 
Hi Dave,

There is no StringReader class member to know what line you are on or
starting a read at a certain line with a StreamReader.

You could use a counter variable and increment it each time you read a line.
When the counter equals the line you want, you have your line.

Another suggestion is to read in the entire file, use the VB Split funtion
(not the .NET String Split method) to split the string you read in into an
array of strings using the vbCrLf character, and put the results in a string
array.

However, both of these suggestions are not optimal. You may want to use a
different System.IO class and file format that gives you more control of
reading from a certain point in the file.

Maybe if you share what you are trying to accomplish someone here can help.


--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
 
Dave said:
If I want to determine the line number in the text file that I am
currently reading (In a StreamReader.ReadLine process), how can I do that?

You'll have to add a counter that coounts how often you called 'ReadLine'.
If I want to start a StreamReader.ReadLine process at a certain line
number in a text file, how can I do that?

Call 'ReadLine' several times until the line is reached.
 
Mike,
Another suggestion is to read in the entire file, use the VB Split funtion
(not the .NET String Split method) to split the string

What is your reason for that. I found reading your message this solution not
the best, because the performance from the split function, that I used for
this, was very low. Is there maybe a difference in performance?

Cor
 
Back
Top