Re-opening file streams

A

Andrew Falanga

Hi,

I'm looking through the object browser in VS and I'm not seeing a Re-
Open function in the FileStream class. So, I'm wondering how would
one do that? If I have a FileStream object that I Close( ), can I
simply new another one and assign to the same variable? Like this:


System.IO.FileStream fs = new System.IO.FileStream("somefile");

// doing important stuff

fs.Close( ); // releases file handles and such

// doing other important stuff

fs = new System.IO.FileStream("somefile");


Can that be done with the same FileStream object or does C# not allow
that?

Andy
 
J

Jeff Johnson

I'm looking through the object browser in VS and I'm not seeing a Re-
Open function in the FileStream class. So, I'm wondering how would
one do that? If I have a FileStream object that I Close( ), can I
simply new another one and assign to the same variable? Like this:


System.IO.FileStream fs = new System.IO.FileStream("somefile");

// doing important stuff

fs.Close( ); // releases file handles and such

// doing other important stuff

fs = new System.IO.FileStream("somefile");


Can that be done with the same FileStream object or does C# not allow
that?

It's not the same FileStream OBJECT, it's the same FileStream VARIABLE. It's
perfectly acceptable to re-assign a variable, so yes, you can do what you're
trying to do.
 
A

Andrew Falanga

It's not the same FileStream OBJECT, it's the same FileStream VARIABLE. It's
perfectly acceptable to re-assign a variable, so yes, you can do what you're
trying to do.

Great. Thanks. So, now I'm looking through the object browser again
but am not finding the answer to this question, how do I know if the
object is usable/disposed of/whatever? So, I call FileStream.Close( )
and then that object is not usable. Short of try/catch stuff, how
does one determine if an object holds valid references?

Andy
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,

I'm looking through the object browser in VS and I'm not seeing a Re-
Open function in the FileStream class.  So, I'm wondering how would
one do that?  If I have a FileStream object that I Close( ), can I
simply new another one and assign to the same variable?  Like this:

System.IO.FileStream fs = new System.IO.FileStream("somefile");

// doing important stuff

fs.Close( ); // releases file handles and such

// doing other important stuff

fs = new System.IO.FileStream("somefile");

Can that be done with the same FileStream object or does C# not allow
that?

Andy

Hi,

You are creating ANOTHER instance of FileStream, so they will not be
the same instance.
Other than that the answer is yes, that is the way to go (as
FileStream does not support a Open() operation.

also note that you should call Dispose()
 
I

Ignacio Machin ( .NET/ C# MVP )

Great.  Thanks.  So, now I'm looking through the object browser again
but am not finding the answer to this question, how do I know if the
object is usable/disposed of/whatever?

You can use the CanXXX properties
 
J

Jeff Johnson

Great. Thanks. So, now I'm looking through the object browser again
but am not finding the answer to this question, how do I know if the
object is usable/disposed of/whatever? So, I call FileStream.Close( )
and then that object is not usable. Short of try/catch stuff, how
does one determine if an object holds valid references?

Why wouldn't you know this? You're the one calling Close(), aren't you?
 
A

Alan Kelly \(Benchmarx Limited\)

I would suggest not closing the stream in the first place, simply seek to
the beginning if you want to process the file a second time.

Use:

fs.Seek(0, SeekOrigin.Begin);
 
M

Michael J. Ryan

I'm looking through the object browser in VS and I'm not seeing a Re-
Open function in the FileStream class. So, I'm wondering how would
one do that? If I have a FileStream object that I Close( ), can I
simply new another one and assign to the same variable? Like this:

Yes, but I would consider a refactor to using statements..

using (var fs = new System.IO.FileStream("somefile")) {
//do some stuff...
} //implicit close/dispose

//do some other stuff

using (var fs = new System.IO.FileStream("somefile")) {
//do some stuff...
} //implicit close/dispose
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top