473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

StreamWriter closes MemoryStream???

Hello,

I've noticed that after calling the Close method on a StreamWriter, I get
exceptions on any operation I might request on the associated stream (with a
message "Cannot access a closed Stream"). Is this the behaviour I must
expect? Nothing is said on the documentation.
Bob Rock
Jul 21 '05 #1
6 8595
Bob Rock <no***************************@hotmail.com> wrote:
I've noticed that after calling the Close method on a StreamWriter, I get
exceptions on any operation I might request on the associated stream (with a
message "Cannot access a closed Stream"). Is this the behaviour I must
expect? Nothing is said on the documentation.


Yes, it's expected behaviour - but I agree it should be more clearly
documented. The StreamWriter is basically a wrapper, and when you close
the wrapper, it closes the wrapped stream as well. The same goes for
Dispose.

It is *sort* of documented - it says that Close is the same as
Dispose(true) and that "This [Dispose] method invokes the Dispose
method of each referenced object."

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
When calling Close,
the underlying System.IO.Stream is closed.
You should only call Close() on that base stream, when you dont need it
more.

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hello,

I've noticed that after calling the Close method on a StreamWriter, I get
exceptions on any operation I might request on the associated stream (with a message "Cannot access a closed Stream"). Is this the behaviour I must
expect? Nothing is said on the documentation.
Bob Rock

Jul 21 '05 #3
> Yes, it's expected behaviour - but I agree it should be more clearly
documented. The StreamWriter is basically a wrapper, and when you close
the wrapper, it closes the wrapped stream as well. The same goes for
Dispose.

It is *sort* of documented - it says that Close is the same as
Dispose(true) and that "This [Dispose] method invokes the Dispose
method of each referenced object."


Well, one thing that the documentation says is "You must call Close to
ensure that all data is correctly written to the underlying stream".
Yeah, sure, unfortunately I don't see what you can do with a closed stream.
Bob Rock
Jul 21 '05 #4
Bob Rock <no***************************@hotmail.com> wrote:
It is *sort* of documented - it says that Close is the same as
Dispose(true) and that "This [Dispose] method invokes the Dispose
method of each referenced object."


Well, one thing that the documentation says is "You must call Close to
ensure that all data is correctly written to the underlying stream".
Yeah, sure, unfortunately I don't see what you can do with a closed stream.


The point is usually that when you've finished writing to a stream,
you're done with it anyway. In the case of MemoryStream, you can still
call ToArray.

It would certainly be nice to be able to do a "detach" on a
StreamWriter, saying that you no longer need its services with the
stream (and flush, please) but that the stream itself needs to live on.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5
I wrote a section of code at work today to write an XML string to a memory
stream with a streamwriter. After filling the memory stream I closed the
streamwriter and then deserialize my object from the memory stream. It
worked just fine so there must be at least some circumstances where it is
not the intended behavior to close the associated stream when the
streamwriter closes.

Dale
"Bob Rock" <no***************************@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hello,

I've noticed that after calling the Close method on a StreamWriter, I get
exceptions on any operation I might request on the associated stream (with a message "Cannot access a closed Stream"). Is this the behaviour I must
expect? Nothing is said on the documentation.
Bob Rock

Jul 21 '05 #6
"DalePres" <no****@nomail.com> wrote in message
news:uV**************@TK2MSFTNGP12.phx.gbl...
I wrote a section of code at work today to write an XML string to a memory
stream with a streamwriter. After filling the memory stream I closed the
streamwriter and then deserialize my object from the memory stream. It
worked just fine so there must be at least some circumstances where it is
not the intended behavior to close the associated stream when the
streamwriter closes.

Dale


Dale,

would you post the "essential" part of the above code so that I may analyze
that you are doing "differently" to have the MemoryStream instance not
closed?
Thx.
Bob Rock
Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bob Rock | last post by:
Hello, I've noticed that after calling the Close method on a StreamWriter, I get exceptions on any operation I might request on the associated stream (with a message "Cannot access a closed...
4
by: ASP Yaboh | last post by:
I am using StreamWriter's .Writeline() method to create a simple text file. I found that ASCII characters 255 & 254 are being inserted as the first two characters of the file which causes a...
1
by: R.L. | last post by:
See the code below, var 'content ' is suppose to be "Hello!", not "". Who knows why? Thanks ---------------------------------------- string text = "hello!"; MemoryStream stream = new...
6
by: Don | last post by:
I'm having problems working with a streamwriter object. After closing the streamwriter and setting it to Nothing, I try to delete the file it was writing to, but I always get the following error...
3
by: Rob | last post by:
I have a streamwriter that may get closed. The writer is passed into another class by ref and is consumed there. What can happen is that the writer might get closed by the spawning class. Is...
9
by: philip | last post by:
If I execute that : Dim Temp as string = "This is a text" Dim sw As StreamWriter Dim fullFileName as string = "c:\text.txt" sw = New StreamWriter(fullFilename) sw.Write(temp) sw.Close() ...
4
by: Heron | last post by:
Hi, Could someone explain me why the following code doesn't work? The memorystream always remains with length 0. MemoryStream input = new MemoryStream();
1
by: Merk | last post by:
In a 2.0 Windows Forms app... in a static class I have a method named WriteToLocalLog() that writes to a text file on the local machine. I declare a StreamWriter at the class level (not inside...
1
by: MSwanston | last post by:
Hi I need some help with saving retreiving data from the cache, and how best to structure my code. FYI am working in VS2005/under .NET2 Framework. Ok, we have a series of reports that get run via...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.