473,382 Members | 1,180 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,382 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
Nov 16 '05 #1
6 10106
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
Nov 16 '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

Nov 16 '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
Nov 16 '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
Nov 16 '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

Nov 16 '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
Nov 16 '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: Kai Bohli | last post by:
Hi all ! This message has been posted on .net.general, but I reposts it here due to lack of replies. This is my first day with Visual Studio and C#, and I'm trying to send "raw data" to the...
7
by: Donovan | last post by:
I can't believe this is causing me as much difficulty as it is, but I have an Infragistics UltraTreeview control that I want to persist whatever the user has in the tree. It has a method SaveAsXml...
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...
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();
2
by: Mikus Sleiners | last post by:
I'm trying to write new stream from string and i can't figure out why my memory stream instance is null after i have writen to it with stream writer. Here is an example. MemoryStream stream =...
2
by: aa49 | last post by:
Hi all, I was trying to unzip a file into the memory stream instead of writing it to text file. The problem I am facing is, the 13 characters in the file's first line are missing other than that...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.