472,954 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

Need to close FileStream

If I have the following:

fs = new FileStream(xmlFile, FileMode.Open,
System.IO.FileAccess.Read);
sr = new StreamReader(fs);

will sr.Close() also close the fs? Or do I need to close both?

Thanks,

Tom
Jul 30 '08 #1
4 2239
On Jul 30, 12:59*pm, "tshad" <ts...@dslextreme.comwrote:
If I have the following:

* * * * * * fs = new FileStream(xmlFile, FileMode.Open,
System.IO.FileAccess.Read);
* * * * * * sr = new StreamReader(fs);

will sr.Close() also close the fs? *Or do I need to close both?

Thanks,

Tom
Hi,

Why not using a StreamReader from the start (if it's a Xml use
XmlReader instead)

if for some reason you still need to use your code do it like this:
using( FileStream fs = new FileStream(xmlFile, FileMode.Open,
System.IO.FileAccess.Read) ){
using( StreamReader sr = new StreamReader(fs) ){
}

}
It makes sure that the objects are disposed correctly
Jul 30 '08 #2
According to the documentation:

http://msdn.microsoft.com/en-us/libr...r_members.aspx

" Close - Closes the StreamReader object and the underlying stream, and
releases any system resources associated with the reader. (Overrides
TextReader.Close().) "

Andrew Faust

"tshad" <ts***@dslextreme.comwrote in message
news:#p**************@TK2MSFTNGP02.phx.gbl...
If I have the following:

fs = new FileStream(xmlFile, FileMode.Open,
System.IO.FileAccess.Read);
sr = new StreamReader(fs);

will sr.Close() also close the fs? Or do I need to close both?

Thanks,

Tom
Jul 30 '08 #3
Much to my annoyance sr.Close will also close the stream.
Pete
Jul 30 '08 #4
So if I am going to read multiple files,

After the first file, I would do an sr.Close() (which closes fs as well).
So then I would do another:

fs = new FileStream(Anotherfile, FileMode.Open,
System.IO.FileAccess.Read);

And another:

sr = new StreamReader(fs);

but I never need to do an fs.Close() then.

Thanks,

Tom

"Peter Morris" <mr*********@SPAMgmail.comwrote in message
news:eZ****************@TK2MSFTNGP02.phx.gbl...
Much to my annoyance sr.Close will also close the stream.
Pete

Aug 25 '08 #5

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

Similar topics

4
by: Daniel P. | last post by:
I'm using StreamReader to read a text file and sometimes I get an error saying that the file is opened by someone else. I haven't found any info about how to set a flag to tell StreamReader to...
0
by: Edward | last post by:
1. I have an .aspx file which parses an XML and XSL file out to Response.Output. 2. However, after I run it once, the XML file is "locked" and I can't open it until I end the ASP.NET process. 3....
4
by: Dan | last post by:
In the following example, is it necessary to close the FileStream object as well as the StreamWriter object? FileStream fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write,...
0
by: kid4rilla | last post by:
I can successfully write the binary data to an image data type, and successfully retrieve it, but when I attempt to play the file in media player after retrieving it, I get the file type isn't...
0
by: Terry Olsen | last post by:
The code I use is below. If I create a FileStream at Module level and then call it from one of these callback routines, I get the "File is in use by another process" exception. My workaround is...
9
by: Blarneystone | last post by:
Hi, I am using VB.NET and trying to pull data from two different tables in the database. I am using what I think is standard code. But the data I am pulling is like the following: Table1...
46
by: Bruce W. Darby | last post by:
This will be my very first VB.Net application and it's pretty simple. But I've got a snag in my syntax somewhere. Was hoping that someone could point me in the right direction. The history: My...
1
by: weird0 | last post by:
i wrote some lines of code to parse xml file and tested it. It ran correct independently but when i inserted that function in a webservice and called it, it gave errors. The error at present is...
18
by: Eric B. | last post by:
Is it a bad idea to do both once I'm done with an object? Eric B.
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.