StreamWriter, FileStream, StreamReader - How to know if they are closed

M

McFly Racing

I am using a StreamWriter a FileStream and a StreamReader. In the event I
have an error I want to check in my finally block and close these if they
are not already closed. How will I know if they are already closed before
trying to close them again? Thank you.
 
J

Joe Mayo

Hi McFly Racing,

If you happen to call Close twice, it isn't a problem. However, you should
only close the file stream in the finally block. This is exactly what the
finally block is good for.

Joe
 
J

Jon Skeet [C# MVP]

Joe Mayo said:
If you happen to call Close twice, it isn't a problem. However, you should
only close the file stream in the finally block. This is exactly what the
finally block is good for.

And if you use the "using" statement, you don't even need to call Close
explicitly at all :)
 

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