473,386 Members | 1,812 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,386 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 1823
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Cliff Wells | last post by:
Hi, I'm writing an application that needs to know if an Internet connection is available. Basically, I want to have something similar to what a lot of email clients have, where the app can work...
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
13
by: Razzbar | last post by:
Is there any to tell via javascript the "state" of a link? I mean, I'd like to be able to tell if a link has been followed or not. (hehe, I can hear shrill crys of "privacy!" in the distance...)
2
by: Stephen Walch | last post by:
I have seen many examples on the web for downloading attachments ffrom ASP.NET forms using response.WriteBinary, but they all use hard-coded ContentTypes (for example, Response.ContentType =...
1
by: google | last post by:
I'm building a small ASPX page in C# to forward HTTP POST requests to another site. When the other site returns an HTTP error such as 404 or 405, I want to return that same HTTP status value to my...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: tascien | last post by:
Hi guys, when i use winhttp, and the server returns status 500, I get the text that the server returned anyway... when i use webrequest, and the server returns status 500, webrequest just...
1
by: Henning Friese | last post by:
Hello HG, I'm using anonymous pipes for ipc between multiple processes. The pipes are created with win32-functions since there is no support for pipes in the dotnet-framework. To access the pipe...
2
by: Patrick Finnegan | last post by:
Tables may be reorg pending or "set integrity pending". Can I run a script to determine the status of a table?
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: 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: 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
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.