Nick Nick - 1 year ago 208
C# Question

System.ObjectDisposedException: Cannot access a closed Stream

Is it true that this does not necessarily mean the stream has been disposed of by code - either in a using or by calling dispose.

The stream could have been closed outside of this code and this exception would still occur?

Answer Source

So I will make my comment an answer: Yes, a stream could just as well be closed from outside your code, so make sure you check for a System.ObjectDisposedException.

There are several occasions this could happen: imagine for example a stream associated with a network connection and the connection is suddenly interrupted. Depending on the implementation this could close the stream and throw that particular exception if the stream is accessed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download