473,387 Members | 1,590 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,387 software developers and data experts.

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

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.
Nov 16 '05 #1
2 2672
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
--
http://www.csharp-station.com

"McFly Racing" <No****@Microsoft.com> wrote in message
news:em*************@TK2MSFTNGP11.phx.gbl...
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.

Nov 16 '05 #2
Joe Mayo <jm***@nospamAtCSharpDashStation.com> wrote:
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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Páll Ólafsson | last post by:
Hi I keep getting this error if Im using UTF7? An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: Conversion buffer overflow. Only...
9
by: ShadowOfTheBeast | last post by:
Hi, I have got a major headache understanding streamReader and streamWriter relationship. I know how to use the streamreader and streamwriter independently. but how do you write out using the...
4
by: David Buchan | last post by:
Hello, I wonder if anyone could help me. I'm using vb.NET and I'd like to read a binary file, byte by byte, and then write to another file (making a duplicate, identical file). I'd then...
3
by: Rob | last post by:
I have a streamwriter that may get closed. The writer is passed into another class by ref and is consumed there. What can happen is that the writer might get closed by the spawning class. Is...
1
by: iwdu15 | last post by:
hi...just a quick question. what are the differences in using a FileStream and StreamWriter opposed to just a StreamWriter.....for instance Dim fs as New FileStream("C:\Test.txt",...) Dim sw As...
15
by: Zytan | last post by:
I have installed the Visual C# 2005 Code Snippets, and the snippets for file handling use StreamReader and StreamWriter, instead of System.IO.File.*. VB 2005 code snippets don't use these. Why...
2
by: asem0525 | last post by:
I have this kind of exception: System.IO.IOException: The process cannot access the file 'E:\asem\Visual Studio 2005\WebSites\WebSite1\App_Data\Books.txt' because it is being used by another...
5
by: Rob | last post by:
Hi, I have a VB.Net application that parses an HTML file. This file was an MS Word document that was saved as web page. My application removes all unnecessary code generated by MS Word and does...
3
by: stumorgan | last post by:
I'm doing some USB communications in C# and am running into a minor annoyance. I'm using the Windows API CreateFile function to get a SafeFileHandle, which I then stuff into a FileStream and from...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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,...
0
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...

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.