Search code examples
windowswinapiwmi

Identify process using a file


I have been trying to figure out how to programmatically identify the process that has a lock on a particular file. I've searched through the Win32 API and WMI, but so far I can't find anything. I know it's possible - Sysinternals is able to list every resource accessed/locked by every process on the system.

Can anyone drop me a hint?


Solution

  • You can use handle.exe from Sysinternals.

    Something like:

    > handle /accepteula C:\path\to\directory
    ...
    program.exe           pid: 1234   type: File           2E4: C:\path\to\directory
    ...
    

    Thanks to https://stackoverflow.com/a/599268/367916 .