Problems with Streams ?

  • Thread starter Thread starter Phil396
  • Start date Start date
P

Phil396

I keep getting the error message

The process cannot access the file "---"
because it is being used by another process.

Here is the code where it crashes


string[] thefiles = Directory.GetFiles
(m_WorkingDir, "*.DEL");

foreach (string file in thefiles)
{
WriteLogEntry("Deleting : " + (new FileInfo
(file)).FullName);

try { File.Delete(file); }
catch { WriteLogEntry("** Delete failed."); }
}

}
}

I tried running this code in a console app
and it worked fine but in my program it crashes
any ideas of what is causing the problem

I found this code in a string helper class

FileStream myFile = File.Create(fileName);

and thought the File.Create might be causing the
problem but I changed the line to

FileStream myFile = new FileStream(fileName,
FileMode.Create);

However I also tested the code in a console app
and everything worked fine.

Any ideas what might be causing the problem.
 
are you sure that there isn't some other code in the Windows application
that is referencing the file(s) you are attempting to delete. For instance,
I noticed that you are iterating through all of the files that have the .DEL
file extension - are you renaming the file in your application somewhere
else that could be locking the file for some reason?

Also, if the working folder you are iterating through is on a network drive
I have seen file handles hang out for a long time (i.e.. 15 minutes or more)
before finally releasing the file. In those cases I have usually fallen back
to using the windows API for the file operations if I can't resolve the
problem otherwise.

Respond and I'll try to give you some more insight.
 
I just got handed this program today so
I have not been able to study the code in depth.
First off it is not using a network drive.
It is using .Net technologies such as
Windows Services, Files and Streams ( where
I believe the problem is located) and even DTS packages.
I will research the code tonight and be able to give
a better explination of what the problem might be
tomorrow.However, I believe the program worked fine before
and then started to partially crash then work for
a while but now the program will just crash.
The program first deletes four sets of files. It would
start by crashing on the first set and complete the
others. Now it just crashes on all four.
 
If you wish to make sure you dont have any locks on files - go and get pstools from sysinternals (.com), it comes with a huge number of utilities to help with these sorts of problems

Run the program (yours) with debug mode and step through each of the processes to see what files are locked or not

Regards
Jonathan Rucker
 
Back
Top