C# file access violation

G

Guest

I am having trouble with an access violation in my program when I am trying
to clean-up on program exit. I create a temp directory and then i put some
files in it using FileStreams. The file streams are created using this call...

using (FileStream streamWriter = new FileStream(Application.StartupPath +
"\\temp\\" + directoryName + currentFileName, FileMode.Create,
FileAccess.Write, FileShare.None))

and then inside of this "using" call I have a try...finally that has in the
finally the statements...

streamWriter.Flush();
streamWriter.Close();

When I go to delete out my temp folder though I am getting an error that
states...

"An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll

Additional information: The process cannot access the file "EDIFA152"
because it is being used by another process."

I don't see how the streamWriter could not be closed and it looks as if .Net
is leaving this file open for some reason. Maybe some kind of caching
mechanism or delayed clean-up? I thought it might be delayed clean-up, which
is why i put the "using" statement in. So the FileStream should be freed as
soon as it is done. I'm not really sure where to go next with this.

Btw, I am using .Net 1.1 in VS 2003.

Thanks,
Justin Etheredge
 
B

Bob

File IO, especially in XP is slow. The OS does not release
the file fast enough. I have to build a cluge delay in all
IO deletes when I'm done in order to wait for the OS
to let the file go.

int waitCnt = 0;
FileInfo fi = new FileInfo(filename);
while(waitCnt < 10) {
KillMe:
try {
fi.Delete();
} catch {
for(int i=0;i<123311;i++)
Application.DoEvents();
waitCnt++;
}
goto KillMe;
}
 

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