I have some code that has thrown the following exception:
System.IO.IOException: The process cannot access the file 'MyFileName' because it is being used by another process.
at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)
at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)
at System.IO.Directory.Delete(String path, Boolean recursive)
at MyCodeSomewhere...
Typically I know this to mean I've had an exception somewhere and not tidied up the relevant (e.g.) StreamReader
.
Occasionally (only occasionally, typically it's my fault) I get the situation where it's something outside my program has a lock on the file instead (such as my text editor, which is still my fault, but less nasty than a bug caused by me not tidying up after myself).
Is there a technique I can programmatically use to detect which process is causing the file to be unavailable?
- Is it my process?
- Is it another process on this machine? If so what's it's name
- Is it being accessed by a remote computer via file share? If so do I have any chance of finding out relevant information?
What would go in the method GetProcessNameCausingMeGrief
?
catch (IOException ioEx)
{
string processName = GetProcessNameCausingMeGrief(ioEx);
if (processName == Process.GetCurrentProcess())
{
throw new ApplicationException("Oops, this code still has it's own file locked", ioEx);
}
throw;
}