29

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?

0

7 Answers 7

27

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 .

13

You could use Process Explorer from Microsoft

  1. Download & unpack & run Process Explorer
  2. Click Find menu and then click Find Handle or DLL... or press CTRL + F
  3. Copy and paste path to locked folder of file
  4. Click Search, you can kill process from main Process Explorer window
4

If you can limit yourself to new enough versions of Windows, the Restart Manager can tell you which process has a particular file open.

1
  • 1
    @MichaelBest Search the link in the Wayback Machine to get a nicely formatted result, or to see the migrated version here.
    – Neil
    May 13, 2020 at 17:43
2

Because of the way Process Explorer works, I suspect that what you need to look for is a way of finding the file handles attached to a given process, and that you'll have to pull that list for each process in the system and look for your file within it.

0

I don't know in Windows, but somebody might find useful to know that, in Linux, you can use the lsof command, or just search through the folders /proc/PROCESS_ID/fd and see what process has opened the file.

1
  • On Linux this is not much of a problem since inodes are refcounted, so you can remove a file even if someone is holding it open.
    – JesperE
    Dec 19, 2008 at 7:44
0

WhoLockMe is a nice right click windows explorer extension.

0

This article might be helpful to you.

It appears you are forced to search through the list of files for each process on the system using undocumented functions in ntdll.dll.

1
  • 1
    "404 Page Not Found" Sep 7, 2016 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.