Deleting files after reading

B

belmontpress

I wish to delete some files from a directory after reading them but
have the problem that the system says that the files are in use and
cannot delete them even though I have set the StreamReader to null:

DirectoryInfo di = new DirectoryInfo(@"C:\test\");

FileInfo[] fiArray = di.GetFiles("*.txt");

foreach (FileInfo fi in fiArray)
{
Console.WriteLine (fi.Name);

StreamReader sr = new StreamReader(fi.FullName);
sr = System.IO.File.OpenText(fi.FullName);

sr.Close();
sr = null;

fi.Delete();
}
Am I missing something??
 
M

Michael Nemtsev

Hello (e-mail address removed),

GC just doen't collect info and free resource
Call GC.Collect() before fi.Delete and everything will work fine
I wish to delete some files from a directory after reading them but
have the problem that the system says that the files are in use and
cannot delete them even though I have set the StreamReader to null:

DirectoryInfo di = new DirectoryInfo(@"C:\test\");

FileInfo[] fiArray = di.GetFiles("*.txt");

foreach (FileInfo fi in fiArray)
{
Console.WriteLine (fi.Name);
StreamReader sr = new StreamReader(fi.FullName);
sr = System.IO.File.OpenText(fi.FullName);
sr.Close();
sr = null;
fi.Delete();
}
Am I missing something??
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
M

Mike

Michael Nemtsev said:
Hello (e-mail address removed),

GC just doen't collect info and free resource
Call GC.Collect() before fi.Delete and everything will work fine

Invoking the GC is almost never the right solution.
If the the StreamReader is not closing the underlying file resource right
away on the Close(), it probably should be.
(There's also a chance the filesystem could also need a small delay between
the Close and the Delete.)
Also, in this code, if OpenText has any problems, the delete won't ever get
called.
I'd try using the IDispose/using pattern. After the using block the stream
should be closed.

foreach (FileInfo fi in fiArray)
{
try
{
Console.WriteLine (fi.Name);
using ( StreamReader sr = new StreamReader(fi.FullName))
{
sr = System.IO.File.OpenText(fi.FullName);
// ...read the text from sr...
}
}
catch
{
// read problems...
}
// Possibly sleep here for a millisecond or so?? or otherwise twiddle
fi?

try
{
fi.Delete();
}
catch
{
// delete problems...
}
}


I wish to delete some files from a directory after reading them but
have the problem that the system says that the files are in use and
cannot delete them even though I have set the StreamReader to null:

DirectoryInfo di = new DirectoryInfo(@"C:\test\");

FileInfo[] fiArray = di.GetFiles("*.txt");

foreach (FileInfo fi in fiArray)
{
Console.WriteLine (fi.Name);
StreamReader sr = new StreamReader(fi.FullName);
sr = System.IO.File.OpenText(fi.FullName);
sr.Close();
sr = null;
fi.Delete();
}
Am I missing something??
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You are opening the file twice, StreamReader's constructor returns an open
file , ready to be readed, but you are creating another when you call Open

Do this:
StreamReader sr = new StreamReader(fi.FullName);
sr.Close();
fi.Delete();

I assume you deleted your actions (should be between the declaration and the
closing
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,
Invoking the GC is almost never the right solution.

You are right, usually you should let the GC to work as it's intended,
alone.
If the the StreamReader is not closing the underlying file resource right
away on the Close(), it probably should be.
(There's also a chance the filesystem could also need a small delay
between the Close and the Delete.)

Don't think so, Close should return once the API close is performed,
otherwise you may get A LOT of problems
Also, in this code, if OpenText has any problems, the delete won't ever
get called.

The delete is not the problem, the Close is. you can get a open file around
until you either close your program or the instance gets out of scope.

using ( StreamReader sr = new StreamReader(fi.FullName))
{
// ...read the text from sr...
}

should be more than enough for that.
 
M

Mike

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

You are opening the file twice, StreamReader's constructor returns an open
file , ready to be readed, but you are creating another when you call Open

Yikes, I didn't even pay attention to this obvious redundancy - no more
responding to posts without adequate sleep :)

I can almost see that a compile warning should be created here, something
like:

Warning: New object assigned to "sr" on line 121 but never used before
re-assignment on line 123.

This warning could occur if:
- the variable is not maked volatile
- a new class is created on the heap
- a new struct is created that is larger than the lagest simple
value-type
- a new struct is created (regardless of size) with a "non-trivial" ctor
- the new object is never used

so basically:
- anything that's potentially expensive to create and never gets used
- is likely a programmer error (as in this case)


thanks,
m
 
M

Michael Nemtsev

Hello Ignacio Machin ( .NET/ C# MVP )" ignacio.machin AT dot.state.fl.us,

I understand that GC needn't to be called in this case,
but experimented a bit I've found that nothing else works in my enviroment
except GC.Collect

I've tried the disposal patterns and any other cases, but none helps

What's the most interesing, the EXE built on another PC works fine


I> Hi,
I>I> You are right, usually you should let the GC to work as it's
I> intended, alone.
I>I> Don't think so, Close should return once the API close is performed,
I> otherwise you may get A LOT of problems
I>I> The delete is not the problem, the Close is. you can get a open file
I> around until you either close your program or the instance gets out
I> of scope.
I>
I> using ( StreamReader sr = new StreamReader(fi.FullName))
I> {
I> // ...read the text from sr...
I> }
I> should be more than enough for that.
I>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top