471,347 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

determining the status of a stream

Is there some way to find out if a stream is already open? In
particular, I want to be able to switch from a NetworkStream to a
FileStream depending on input. I know I can use a Stream object and
cast it up, but how do I find out if the stream has been closed?

The pseudocode would go a little something like this:

public void OpenNetConnection(Socket connection)
{
if the stream is closed
open a new NetworkStream based on the connection Socket.
else
throw an exception
}

public void OpenFileConnection(string filename)
{
if the stream is closed
open a FileStream to the file
else
throw an exception
}

Lee Crabtree
Feb 10 '06 #1
1 1737
Lee Crabtree wrote:
Is there some way to find out if a stream is already open? In
particular, I want to be able to switch from a NetworkStream to a
FileStream depending on input. [...]

public void OpenNetConnection(Socket connection) {
if the stream is closed
open a new NetworkStream based on the connection Socket.
else
throw an exception
}

public void OpenFileConnection(string filename) {
if the stream is closed
open a FileStream to the file
else
throw an exception
}


Hi Lee. The short answer is, no, there is no method or property on the
abstract Stream class supporting this query. The simplest solution is to set
your Stream reference to null when you close it - then later you can test it
against null to see if it's closed. By restructuring your methods you may
also be able to statically ensure the property that the stream is closed
when the new stream is opened, so that an exception is not necessary; for
example, you might only have an operation which closes the existing stream
and opens a new stream. Note that while it's possible to have a single
Stream reference refer to multiple kinds of streams over its lifetime, each
of these must be represented by a different runtime object. I hope this
helps.
--
Derrick Coetzee, MCAD, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights.
Feb 13 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Cliff Wells | last post: by
66 posts views Thread by Darren Dale | last post: by
13 posts views Thread by Razzbar | last post: by
2 posts views Thread by Stephen Walch | last post: by
2 posts views Thread by Mike Collins | last post: by
reply views Thread by tascien | last post: by
1 post views Thread by Henning Friese | last post: by
2 posts views Thread by Patrick Finnegan | last post: by
reply views Thread by Ronak mishra | last post: by

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.