By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,660 Members | 1,953 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,660 IT Pros & Developers. It's quick & easy.

streamwriter and streamreader and closing files

P: n/a
After I read or write a file with streamreader and streamwriter and I close
it with the Close method, does that automatically let go of the file so that
any other process can modify it?
In my form, I read from a small configuration file with streamreader and
close it with streamreader.Close(). In this same event, when I open it for
modification with a streamwriter, it tells me the file's being used by
another process.

Thanks again.
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
With my limited knowledge of the garbage collector, I'm going to
venture an educated guess that no, the file is not immediately
released.

From what I understand, whenever you call .Close() you're essentially
telling the GC that you don't want this file anymore and the GC will
release it when it gets around to it. (which is not necessarily
immediately)

And just a tip that I've found handy, for objects which implement
IDipsose (such as streamwriter/reader), you can use the using
statement which will automatically displose the stream for you.

that way you don't have to remember to .Close()

using(StreamReader sr = new StreamReader("myfile.txt"))
{
// do my stuff with the stream, and no matter if an exception
occurs, i manually "return", or i reach the end of the block, my
stream gets disposed for me.
}

On Fri, 6 Feb 2004 09:30:15 -0500, "Jimbo" <None> wrote:
After I read or write a file with streamreader and streamwriter and I close
it with the Close method, does that automatically let go of the file so that
any other process can modify it?
In my form, I read from a small configuration file with streamreader and
close it with streamreader.Close(). In this same event, when I open it for
modification with a streamwriter, it tells me the file's being used by
another process.

Thanks again.


Nov 15 '05 #2

P: n/a
VM
Hi,
Thanks for the info. I tried that and it works great. Like you said,
apparently the file's not released immediately when you use Close(). But I
switched it your way (and eliminated all the Close() ) and now it doesn't
give me the problem anynore.

Thanks.
"Alfred Taylor" <om***@nospam.yahoo.com> wrote in message
news:tp********************************@4ax.com...
With my limited knowledge of the garbage collector, I'm going to
venture an educated guess that no, the file is not immediately
released.

From what I understand, whenever you call .Close() you're essentially
telling the GC that you don't want this file anymore and the GC will
release it when it gets around to it. (which is not necessarily
immediately)

And just a tip that I've found handy, for objects which implement
IDipsose (such as streamwriter/reader), you can use the using
statement which will automatically displose the stream for you.

that way you don't have to remember to .Close()

using(StreamReader sr = new StreamReader("myfile.txt"))
{
// do my stuff with the stream, and no matter if an exception
occurs, i manually "return", or i reach the end of the block, my
stream gets disposed for me.
}

On Fri, 6 Feb 2004 09:30:15 -0500, "Jimbo" <None> wrote:
After I read or write a file with streamreader and streamwriter and I closeit with the Close method, does that automatically let go of the file so thatany other process can modify it?
In my form, I read from a small configuration file with streamreader and
close it with streamreader.Close(). In this same event, when I open it formodification with a streamwriter, it tells me the file's being used by
another process.

Thanks again.

Nov 15 '05 #3

P: n/a

"VM" <vo******@yahoo.com> wrote in message
news:ef*************@tk2msftngp13.phx.gbl...
Hi,
Thanks for the info. I tried that and it works great. Like you said,
apparently the file's not released immediately when you use Close(). But I
switched it your way (and eliminated all the Close() ) and now it doesn't
give me the problem anynore.

Thanks.


According to Richter's (brilliant) book, the IL produced from using the
"using" keyword like this:

using (FileStream fs = new FileStream("myfile.txt", FileMode.Open)
{
// do something useful...
}

is identical to

FileStream fs = null;
try
{
fs = new FileStream("myfile.txt", FileMode.Open);
// do something useful...
}
finally
{
if (fs != null) ((IDisposable) fs).Dispose();
}

It's just syntactic sugar to make the code more readable. The important part
is that the code inside finally is guaranteed to be called, even if an
exception should occur. Note by the way that Dispose is called, not Close.
The reason is that Dispose is part of IDisposable (making "using" possible),
wheras Close is not. However, as far as I know, Close just calls Dispose
anyways.

Regards,
Einar
Nov 15 '05 #4

P: n/a

Hi Jimbo,

Is your problem resolved?

Just as Einar said, the using Statement in C# is the same as calling
IDisposable.Dispose in finally block.
IDisposable.Dispose method will explicitly release and free the unmanaged
resources, such as files, streams, and handles held by an instance of the
class.
For more detailed information, please refer to:
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfSystemIDisposableClassDisposeTopic.asp

If you have any further concern, please feel free to post, I will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #5

P: n/a
Alfred Taylor <om***@nospam.yahoo.com> wrote:
With my limited knowledge of the garbage collector, I'm going to
venture an educated guess that no, the file is not immediately
released.

From what I understand, whenever you call .Close() you're essentially
telling the GC that you don't want this file anymore and the GC will
release it when it gets around to it. (which is not necessarily
immediately)


No - calling Close on a StreamWriter (etc) is effectively the same as
calling Dispose. Otherwise Close() wouldn't do anything at all - the GC
doesn't need to be told that you don't want this file, it'll just
notice (eventually) after the object can no longer be referenced, and
then the finalizer will run at some point.

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

P: n/a
SM
Try using Thread.Sleep(200) before your second call...
"Jimbo" <None> wrote in message news:<eJ**************@TK2MSFTNGP11.phx.gbl>...
After I read or write a file with streamreader and streamwriter and I close
it with the Close method, does that automatically let go of the file so that
any other process can modify it?
In my form, I read from a small configuration file with streamreader and
close it with streamreader.Close(). In this same event, when I open it for
modification with a streamwriter, it tells me the file's being used by
another process.

Thanks again.

Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.