Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MemoryStream - Cannot access a closed Stream

Hi why using (var sw = new StreamWriter(ms)) returns Cannot access a closed Stream exception. Memory Stream is on top of this code.

using (var ms = new MemoryStream()) {     using (var sw = new StreamWriter(ms))     {                          sw.WriteLine("data");         sw.WriteLine("data 2");         ms.Position = 0;         using (var sr = new StreamReader(ms))         {             Console.WriteLine(sr.ReadToEnd());                                 }     } //error here } 

What the best way to fix it ? Thanks

like image 380
Arbejdsglæde Avatar asked Jun 07 '12 15:06

Arbejdsglæde


People also ask

Can not access a closed Stream C#?

Hi why using (var sw = new StreamWriter(ms)) returns Cannot access a closed Stream exception . Memory Stream is on top of this code. This may be because you are creating a StreamWriter and StreamReader from the same MemoryStream. You might try using two different MemoryStreams: one for reader and one for writer.

What is the difference between Stream and MemoryStream?

You would use the FileStream to read/write a file but a MemoryStream to read/write in-memory data, such as a byte array decoded from a string. You would not use a Stream in and of itself, but rather use it for polymorphism, i.e. passing it to methods that can accept any implementation of Stream as an argument.

Do I need to close MemoryStream?

You needn't call either Close or Dispose . MemoryStream doesn't hold any unmanaged resources, so the only resource to be reclaimed is memory. The memory will be reclaimed during garbage collection with the rest of the MemoryStream object when your code no longer references the MemoryStream .

How do I open memory Stream?

MemoryStream creates streams that have memory as a backing store instead of a disk or a network connection. This can be useful in eliminating the need to write temporary files to disk or to store binary blob information in a database. To open or read file we use FileStream.


1 Answers

This is because the StreamReader closes the underlying stream automatically when being disposed of. The using statement does this automatically.

However, the StreamWriter you're using is still trying to work on the stream (also, the using statement for the writer is now trying to dispose of the StreamWriter, which is then trying to close the stream).

The best way to fix this is: don't use using and don't dispose of the StreamReader and StreamWriter. See this question.

using (var ms = new MemoryStream()) {     var sw = new StreamWriter(ms);     var sr = new StreamReader(ms);      sw.WriteLine("data");     sw.WriteLine("data 2");     ms.Position = 0;      Console.WriteLine(sr.ReadToEnd());                         } 

If you feel bad about sw and sr being garbage-collected without being disposed of in your code (as recommended), you could do something like that:

StreamWriter sw = null; StreamReader sr = null;  try {     using (var ms = new MemoryStream())     {         sw = new StreamWriter(ms);         sr = new StreamReader(ms);          sw.WriteLine("data");         sw.WriteLine("data 2");         ms.Position = 0;          Console.WriteLine(sr.ReadToEnd());                             } } finally {     if (sw != null) sw.Dispose();     if (sr != null) sr.Dispose(); } 
like image 195
Thorsten Dittmar Avatar answered Sep 22 '22 04:09

Thorsten Dittmar