StreamReader - reset to the top

samshiell

Member
Joined
Oct 26, 2004
Messages
10
Programming Experience
10+
Hi

When using StreamReader how can you reset the "pointer" to the beginning?

I'm reading some lines then, when a certain condition is met I want to read the whole file.

I've tried setting the BaseStream.Position = 0 but that acheives nothing.

This is my (extremely simplified) code....

fReadIn = New StreamReader(strFileName)
strLine = fReadIn.Readline
strLine = fReadIn.Readline
''.... various stuff and conditions happen here
fReadIn.BaseStream.Position = 0
strFileContents = fR.ReadToEnd

Any ideas?

Thanks

Sam
 
fReadIn.Close
fReadIn = new StreamReader(strFileName)

will close the read, then re-open it (which puts it back at the beginning of the file)
 
fReadIn = New StreamReader(strFileName)
strLine = fReadIn.Readline
strLine = fReadIn.Readline
''.... various stuff and conditions happen here
fReadIn.BaseStream.Position = 0
strFileContents = fR.ReadToEnd

fReadIn.BaseStream.Seek(0, SeekOrigin.Begin) 'will return the reader to the begining
 
fReadIn.BaseStream.Seek(0, SeekOrigin.Begin) 'will return the reader to the begining
"Seek(0, SeekOrigin.Begin)" is equivalent to "Position = 0", neither will work as is because the StreamReader uses an internal buffer and read position. This will work however if you pair it with a call to DiscardBufferedData method to reset the StreamReader buffers.
 
Good grief, I posted this original question 5 years ago (October 2004) and have absolutely no idea now of what I was trying to achieve, why I was doing or what I did in the end.

Thanks anyway for your contribution

Regards

Sam
 
Back
Top