Test if Stream Object is Open

Bernie

Well-known member
Joined
Aug 13, 2007
Messages
98
Programming Experience
3-5
Is there a way to test if a streamreader or streamwrite is open? My application is something like this;

If oStreamReader is Open Then
Read line from file
Else
oStreatReader.Open
Read line from file
End If

Does this kind of test of open or not exist for stream objects?

Thanks,
Bernie
 
It is good practice to close your steams when done with them. This why you will see examples where they will use the keyword Using, because the End Using will dispose of the stream objects for you. If you want check if your file exists use the FileInfo class.
VB.NET:
Dim fi As New FileInfo(<yourpathtoyourfile>)
If fi.Exists Then
'open your stream
 
Last edited:
You can't open an existing StreamReader. They are opened when you create them and once they are closed they are no longer of use. There should never be a need to perform such a test because noone should ever be passing a closed StreamReader to a method because it would be useless.
 
jmcilhinney,

I never said anything about reopening a stream. I wanted to check if it was open and if not to open it.

The other application is in recover from a trapped error. If I have a file stream open inside a try block, it would be nice to be able to test if it is still open when we hit the catch block. If it's open, then close it, rather than just leaving it there.

Bernie
 
I never said anything about reopening a stream. I wanted to check if it was open and if not to open it.
Yeah, you did.
VB.NET:
Else
oStreatReader.Open
The other application is in recover from a trapped error. If I have a file stream open inside a try block, it would be nice to be able to test if it is still open when we hit the catch block. If it's open, then close it, rather than just leaving it there.
There shouldn't be a need. You should create FileStreams, StreamReaders, etc. with a Using block. When execution exits the block the object is disposed, whether an exception was thrown or not. This code snippet will close the file no matter what:
VB.NET:
Try
    Using sr As New StreamReader("file path here")
        'Use sr here.
    End Using
Catch ex As Exception
    MessageBox.Show(ex.ToString())
End Try
If an exception is thrown inside the Using block then, to get to the Catch block, execution must leave the Using block, thereby disposing the StreamReader object.
 
Bernie said:
Does this kind of test of open or not exist for stream objects?
Yes, Stream has a CanRead property that returns False if it is closed. You can access the Stream of the StreamReader from its BaseStream property.
VB.NET:
If reader.BaseStream.CanRead Then
 
Yes, Stream has a CanRead property that returns False if it is closed. You can access the Stream of the StreamReader from its BaseStream property.
VB.NET:
If reader.BaseStream.CanRead Then
Yes and no. It should first be noted that a StreamReader is NOT a Stream. It's a TextReader that reads from a Stream. The original question asked about stream objects and then used a StreamReader in code, so that's one thing to get straight.

If you're talking about a Stream object specifically then what JohnH says is correct:
VB.NET:
Dim file As New IO.FileStream("file path here", IO.FileMode.Open)

MessageBox.Show(file.CanRead.ToString)

file.Close()

MessageBox.Show(file.CanRead.ToString)
Try that with an implicitly created Stream and a StreamReader though and you encounter an issue:
VB.NET:
Try
    Dim reader As New IO.StreamReader("file path here")

    MessageBox.Show(reader.BaseStream.CanRead.ToString)

    reader.Close()

    MessageBox.Show(reader.BaseStream.CanRead.ToString)
Catch ex As Exception
    MessageBox.Show(ex.ToString)
End Try
When a StreamReader is explicitly closed its BaseStream property is set to Nothing, so you have no access to the underlying FileStream after that. Only if you closed the BaseStream rather than the StreamReader would you still have access to the Stream:
VB.NET:
Dim reader As New IO.StreamReader("D:\John\Documents\RebuildIconCache.bat")

MessageBox.Show(reader.BaseStream.CanRead.ToString)

reader.BaseStream.Close()

MessageBox.Show(reader.BaseStream.CanRead.ToString)
That's never going to happen with an implicitly created FileStream though.
 
Back
Top