What happens: some process opens the file for exclusive access (sharing not allowed) which is the default mode. An attempt to open the same file from your process fails with the exception you observe. The type of this exception is
System.IO.IOException
. Unfortunately, the exception does not contain the handle of the process holding the file.
Nothing can be done until the process holding the file exits. You can terminate this process; this is all you can do.
You can get the handle of the offending process by enumerating all the processes using
System.Diagnostics.Process.GetProcesses
; somehow identify the process you need using the instance of the
Process
obtained during enumeration. You will get the instance of the process. Use
Process.Kill
to terminate it.
This way is problematic because there is no a universal criterion used to identify the process. You need only the process which has opened your file. You can do it using
System.Management
. Here is how:
http://www.pcreview.co.uk/forums/re-get-files-opened-process-t1340888.html[
^]. Using this code sample you can find out which process is holding your file to terminate it.
—SA