views:

1362

answers:

3

I have a unit test that works fine locally but when uploaded to TeamCity build server fails with "The process cannot access the file because it is being used by another process."

  1. Before I do anything in the Test I check in the setup if the file exists and if so try to delete it. This fails with the same error message as above
  2. When wriitng the file, I close the writer then dispose of it which I believe should get rid of any resources.

So I have a couple queries

  1. Has anyone had similar issues and manage to get around them
  2. How can find out programticall what process has selfishly locked the file!!!

Cheers

+4  A: 

Sysinternals handle.exe is the first step. Or use Process Explorer's Find Handle or DLL. It's always safer to kill the process than close the handle, though.

Programmatically, it's a matter of writing a kernel driver that reads the Windows handle table.

Mark
+2  A: 

ProcessMon (for finding) and Unlocker (at least for unlocking) are useful too.

StingyJack
+1  A: 

Sysinternals FileMon has some information (scroll down) on how they accomplished this in older windows versions.

Process monitor is their latest version, but it doesn't give many clues as to how they do it. There are some knowledge base articles linked there, though, that should lead you to this information.

Adam Davis