user47589
user47589

Reputation:

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?

Upvotes: 30

Views: 62510

Answers (7)

jtomaszk
jtomaszk

Reputation: 11161

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

Upvotes: 14

Neil
Neil

Reputation: 55382

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

Upvotes: 4

thejoshwolfe
thejoshwolfe

Reputation: 5504

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 .

Upvotes: 28

Mathias
Mathias

Reputation: 34251

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.

Upvotes: 0

AndreasN
AndreasN

Reputation: 2897

WhoLockMe is a nice right click windows explorer extension.

Upvotes: 0

Marc
Marc

Reputation: 1356

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.

Upvotes: 0

chaos
chaos

Reputation: 124267

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.

Upvotes: 2

Related Questions