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

Opening a file for both read/write

P: n/a
In C I can open a file as readable, writable, or both. Then I can
arbitrarily do either operation without closing the file between operations
as long as I perform some kind of file position indicator repositioning when
I change between reading and writing. In C# I'd like something like a
StreamReaderWriter that I could use the same way, but there doesn't appear to
be such a thing. I don't want to have to repeatedly close, reopen, and
reseek since I in my application I would have to do this for each byte that I
write. Is it possible to open the same file with both a StreamReader and a
StreamWriter simultaneously?

Thanks!
Ray
Aug 21 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Ray,

Assuming that you were holding a reference to the underlying FileStream
(or, you used the BaseStream property on the StreamReader or StreamWriter
class), you could have a StreamReader/StreamWriter open at the same time.
You would have to call Flush on the StreamWriter, to flush any buffered data
to the underlying stream, and then call DiscardBufferedData on the
StreamReader, to discard any data that the reader buffered. Then, you could
seek on the file stream, and you should be able to use the
StreamReader/StreamWriter again.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ray Mitchell" <Ra*****************@MeanOldTeacher.comwrote in message
news:B8**********************************@microsof t.com...
In C I can open a file as readable, writable, or both. Then I can
arbitrarily do either operation without closing the file between
operations
as long as I perform some kind of file position indicator repositioning
when
I change between reading and writing. In C# I'd like something like a
StreamReaderWriter that I could use the same way, but there doesn't appear
to
be such a thing. I don't want to have to repeatedly close, reopen, and
reseek since I in my application I would have to do this for each byte
that I
write. Is it possible to open the same file with both a StreamReader and
a
StreamWriter simultaneously?

Thanks!
Ray

Aug 21 '07 #2

P: n/a
Hi,

"Ray Mitchell" <Ra*****************@MeanOldTeacher.comwrote in message
news:B8**********************************@microsof t.com...
In C I can open a file as readable, writable, or both. Then I can
arbitrarily do either operation without closing the file between
operations
as long as I perform some kind of file position indicator repositioning
when
I change between reading and writing. In C# I'd like something like a
StreamReaderWriter that I could use the same way, but there doesn't appear
to
be such a thing. I don't want to have to repeatedly close, reopen, and
reseek since I in my application I would have to do this for each byte
that I
write. Is it possible to open the same file with both a StreamReader and
a
StreamWriter simultaneously?
Use FileStream

Aug 21 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.