Determining what process has a lock on a file

耗尽温柔 提交于 2019-12-06 04:42:01

问题


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


回答1:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/305843/determining-what-process-has-a-lock-on-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!