Re-read stream

M

Morten.Snedker

How do I use a StreamReader more than once?

Dim rs As New StreamReader(fd.FileName, Encoding.Default)
Do While rs.Peek() >= 0
...
Loop

'Loop 2
Do While rs.Peek() >= 0
...
Loop

Now loop 2 doesn't work 'cause I've gotten to then end of the stream.
How do I "re-initialize", so I can perform another loop through the
stream?

Regards /Snedker
 
J

Jon Skeet [C# MVP]

How do I use a StreamReader more than once?

Dim rs As New StreamReader(fd.FileName, Encoding.Default)
Do While rs.Peek() >= 0
...
Loop

'Loop 2
Do While rs.Peek() >= 0
...
Loop

Now loop 2 doesn't work 'cause I've gotten to then end of the stream.
How do I "re-initialize", so I can perform another loop through the
stream?

The simplest way would be to close the StreamReader and re-open it. You
*can* call DiscardBufferedData, and then set BaseStream.Position = 0 as
an alternative though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top