By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,032 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,032 IT Pros & Developers. It's quick & easy.

Reusing a streamreader

P: n/a
'if you do this
Dim sr As New StreamReader
_(System.AppDomain.CurrentDomain.BaseDirectory& "CR.txt")

' and then loop through values

while sr.peak > -1
......
end while

sr.close

is there a way to reopen the streamreader on the same file?
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Peter" <pe***@mclinn.com> wrote in message news:dc**************************@posting.google.c om...
'if you do this

' and then loop through values

while sr.peak > -1
.....
end while

sr.close

is there a way to reopen the streamreader on the same file?


Only,

sr = New StreamReader( AppDomain.CurrentDomain.BaseDirectory & "CR.txt")

When you call Close( ) on the StreamReader, it will have disposed
of it's system resources (i.e., file handle, and this is a _good_ thing).

OTOH, if you hadn't called Close( ) yet, then you could say,

sr.BaseStream.Position = 0

provided System.Diagnostics.Debug.Assert( sr.BaseStream.CanSeek),
which is ordinarily true for FileStreams as in this case; and start reading
over again. In this case, you're always holding onto the file handle, tho.

I'd just re-create the StreamReader. If your concern is remembering
the file directory and name originally used to open the StreamReader,
then save that into a String variable and carry that with you alongside
sr (it's also in CType(sr.BaseStream,System.IO.FileStream).Name
until sr has been closed).
Derek Harmon
Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.