views:

45

answers:

3

I keep getting that exception in my program. How do I know which process locked it, either by program code or by doing something with windows?

System.IO.IOException : The process cannot access the file 'file.ext' because it is being used by another process.

+1  A: 

Have a look at this: http://ccollomb.free.fr/unlocker/

I use it all the time, tells you which process has locked it and gives you the chance to unlock it fully. Useful especially for when your debugging image manipulation apps and it bombs out without closing the file properly.

Hope this helps!

Sean

seanxe
+1  A: 

You can use a tool like ProcessMon to see what process is locking a file.

Oded
it's no longer available for download
Louis Rhys
@Louis RH - didn't realize. Answer updated...
Oded
+3  A: 

You can use ProcessExplorer to search for the file:

Just run that, hit Ctrl-F and type in the name of the file which is locked - it will find all open handles which match the given name, and tell you which process it belongs to.

Kragen
how do I know which process is currently using the file? because when I tried it it looks like it list all recent accesses
Louis Rhys
@Louis RH: after starting of the Process Explorer under an account with administrative rights you should press Ctrl-F or choose in the menu "Find" / "Find Handle or DLL..." and then type the name of the file which you search. So just do exactly what Kragen wrote for you.
Oleg
@Louis - It may be that processes that access that file don't close it immediately, in which case Process Explorer will find those handles / processes for you - I'm not sure how to work out which of those handles is blocking your use of that file however (not all Handles will block access to a file - two processes can share the same file depending on the access that each process requested - read/write etc...)
Kragen