473,412 Members | 2,281 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,412 software developers and data experts.

Closing a stream(easy question)

Hi,
simple question

XmlDocument doc = new XmlDocument();
FileStream file = new FileStream
(PathAndFileName,FileMode.Open,FileAccess.Read,Fil eShare.ReadWrite);
doc.Load(file);

should I now call file.close or do I not need to??

Thanks
Nick
Nov 16 '05 #1
6 7506
Enclose the doc.Load(...) statement in the try-finally block and add the
following in the "finally" clause:

try
{
file.Close();
}
catch(IOException)
{
// Optionally add something like this here:
// Debug.WriteLine("Failed to close the file");
}

"Nick" <ni*********@hotmail.com> wrote in message
news:a0**************************@posting.google.c om...
Hi,
simple question

XmlDocument doc = new XmlDocument();
FileStream file = new FileStream
(PathAndFileName,FileMode.Open,FileAccess.Read,Fil eShare.ReadWrite);
doc.Load(file);

should I now call file.close or do I not need to??

Thanks
Nick


Nov 16 '05 #2
> should I now call file.close or do I not need to??

yes you should call file.Close(). the whole stream is read in the Load()
method and it's not necessary to leave the file opened.

Chris
Nov 16 '05 #3
Hi Nick,

Yes, you should call file.Close(); XmlDocument.Load will not close the stream itself. In any case, calling file.Close() on an already closed file will just be ignored and will do no harm.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4
> Yes, you should call file.Close(); XmlDocument.Load will not close the
stream itself. In any case, calling file.Close() on an already closed file will just be ignored and will do no harm.


after calling close on a FileStream, it will likely become null. the
Close() method of FileStream calls Dispose(). I haven't tested it, but I'm
fairly sure it will be null in all cases.

Chris
Nov 16 '05 #5
Calling Close() will release any resources tied to it, in this case the file. The FileStream object will remain intact however, so you can safely call Close() as many times as you want.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #6
Chris <ct******@hotmail.com> wrote:
Yes, you should call file.Close(); XmlDocument.Load will not close the
stream itself. In any case, calling file.Close() on an already closed file will
just be ignored and will do no harm.


after calling close on a FileStream, it will likely become null.


No it won't. Objects don't just disappear because you call Close on
them. If you still have a reference to an object, that reference isn't
going to become null on its own.
the Close() method of FileStream calls Dispose(). I haven't tested it, but I'm
fairly sure it will be null in all cases.


Could you clarify exactly what you think "will be null"?

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

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

Similar topics

2
by: Sonny | last post by:
Hi experts, I am writing a small code to read input from a file, for example my input file is: X Y Z 26.0 28.0 0.0 32.0 8.0 0.0 My code is:...
5
by: Riku Jarvinen | last post by:
Hi everyone, I asked another question regarding this same subject about a week ago. See thread: ...
3
by: Grant Harmeyer | last post by:
I have an XSL file that is being applied to an RSS feed, and it works great except for 2 things: 1.) XSL doesn't close the <img> tag for xhtml even though I have it being closed in the XSL file...
5
by: John Salerno | last post by:
Is this valid: string allSwitchValues = readSwitches.ReadToEnd().Split('|'); I have no way to test it just yet (although it didn't seem to create compile errors). Originally I had it divided...
2
by: **Developer** | last post by:
I have a UserControl on a form. The form is closing and I need the usercontrol to write to the Registry. Where do I do that? In the Disposed event? I need to do it when controls on the...
4
by: Paul Hemans | last post by:
Hi newbie here. I am learning c++/cli I by writing console apps. When my apps finished they used to display a message at the bottom of the screen, something like "press any key to continue" so that...
2
by: john | last post by:
In TC++PL 3, on page 616, "class basic_ios" has the public members "operator void *() const; // nonzero if !fail() bool operator!() const { return fail(); }" for using them in conditional...
5
by: TBass | last post by:
Hi, I'm moving a socket library I wrote from C to C++. In the C version, I had to malloc char arrays to store incoming communication. My hope was to use std::string in C++, but then I realized a...
6
by: klaas | last post by:
Hi, i'am writing a program in vb.net 2005 based on an access database. Now, i want to close my program automaticly when the user is inactive to a period of eg 10 minutes. I want to close all mine...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.