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. 6 19938
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.
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.
"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
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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: dustin lee |
last post by:
Over the years I've gotten out of the habit of explicitly closing file
objects (whether for reading or writing) since the right thing always
seems to happen auto-magically (e.g. files get written...
|
by: Henrik Holm |
last post by:
I have recently started playing around with Python. Some of the things
I have done have involved reading files. The way I do this is along the
lines of
f = file('file.txt')
lines =...
|
by: Joecx |
last post by:
Using vb.net, I am using Streamreader to read a text file and searching for a
line to delete, the I close the file and open it as a streamwriter so I can
put the new file back to disk without the...
|
by: ext237 |
last post by:
Hello.
I am trying to open a filestream using the following code:
Dim myStreamReader As StreamReader = New StreamReader( _
New FileStream("H:\files\file.txt", FileMode.Open), _
...
|
by: Qwerty |
last post by:
How would I go about doing this? I want to be able to go through a
target directory, find files with a certain format (.toc), and then I
want to write new data to a line inside of the file.
I...
|
by: Penk |
last post by:
I need to know if there's any method or procedure that could make me able to
close all references to a file.
I tried to delete the file but i can't because it's being used by another
process. I...
|
by: Ismaelf |
last post by:
hello!, is there any way that streamreader can be used to read a file
published in a web server, ex: "http://localhost/text.txt"
or is there any other way of reading such file??, thanks!
|
by: Thomas Ploch |
last post by:
Is it defined behaviour that all files get implicitly closed when not
assigning them?
Like:
def writeFile(fName, foo):
open(fName, 'w').write(process(foo))
compared to:
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |