473,582 Members | 3,069 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 OpenNetConnecti on(Socket connection)
{
if the stream is closed
open a new NetworkStream based on the connection Socket.
else
throw an exception
}

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

Lee Crabtree
Feb 10 '06 #1
1 1841
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 OpenNetConnecti on(Socket connection) {
if the stream is closed
open a new NetworkStream based on the connection Socket.
else
throw an exception
}

public void OpenFileConnect ion(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
3632
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 either in "online" or "offline" mode (it keeps a cache of downloaded info, so it can work without a connection if needed). The basic problem is...
66
4948
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
1502
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
4007
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 = "application/pdf"). Is there a good way to "guess" the content type for arbritary file name? Every web server has a mapping from file extension to...
1
6443
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 caller. But when I do so, my caller always reports HTTP error 500. How can I return the actual HTTP status that I received? Here is my code: ...
2
4541
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 set to...it is set to false. Can someone show me what I am doing wrong and tell me the correct way? Thank you. In the page load event, I am...
0
1199
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 throws an exception... How can i find the text that came back with the 500 status? I need this because i am posting to a web service, that returns...
1
1324
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 I create FileStream- objects from the pipe-handles. Reading and writing through those streams works fine. However, I need to know if data is...
2
11879
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
7886
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7809
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8312
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7920
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5685
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1413
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.