Hosam Aly
Hosam Aly

Reputation: 42443

How can I find which process has opened a specific file?

How can I find which processes have a specific file opened, and their open, access and share modes? Additionally, is it possible to change these values for a process? Or is it even possible to open a file for reading if it is already opened for exclusive access by another process?

Please note that I don't want to invalidate the handle of the process having the file opened. I just want to be able to access the file (if possible).

(I'm mainly asking about Windows, but solutions for other platforms are welcome, since they contribute to the community's knowledge.)

Edit: I found some answers for my first question here and there.

Edit 2: Thanks everybody for the tools you mentioned, but I am mainly looking for programmatical techniques (e.g. using Win32 APIs).

Upvotes: 2

Views: 3525

Answers (6)

Gustavo Rubio
Gustavo Rubio

Reputation: 10747

Im not sure if there is a way to do exactly what you want but I do know that using System.Diagnostics.Process class (at least in .Net) you can open processes and watch for certain properties:

System.Diagnostics.Process[] procArray = System.Diagnostics.Process.GetProcessesByName("notepad");
foreach (System.Diagnostics.Process proc in procArray) {
    //do something with the process...
}

Look around the Process class, maybe there is a property or collection to get the data you are looking for.

Upvotes: -1

Drejc
Drejc

Reputation: 14286

It's the lsof command under Linux systems.

Upvotes: 1

Nathan Fellman
Nathan Fellman

Reputation: 127428

For unix you can use fuser:

lnx0:i386_linux26> fuser -v a.cpp

                     USER        PID ACCESS COMMAND
a.cpp                nabcdefg    3952 f....  less

Upvotes: 1

Healthcarel
Healthcarel

Reputation:

For windows, I know about a Tool from Sysinternal (www.sysinternals.com): handle.exe.

Upvotes: 3

Kapil
Kapil

Reputation: 9959

There is an utility called Unlocker which tells you which process has got the lock on a resource .

Upvotes: 2

Mitch Wheat
Mitch Wheat

Reputation: 300489

Process Monitor

Process Explorer

Upvotes: 5

Related Questions