2

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).

Community
  • 1
  • 1
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
  • @Hosam Aly: "but I am mainly looking for programmatical techniques ": perhaps you should put that in the question up front next time? – Mitch Wheat Feb 01 '09 at 09:13
  • @Mitch, thank you. I'm sorry about that. I'll be more careful next time. – Hosam Aly Feb 01 '09 at 09:14
  • 1
    Apparently the [restart manager](http://blogs.msdn.com/b/oldnewthing/archive/2012/02/17/10268840.aspx) can do it for Windows Vista and later – Conrad Frix Feb 17 '12 at 20:39
  • 1
    Instead of starat manager you can use ntdll.dll to get all open handles and NTQueryFileInformation to get paths: http://stackoverflow.com/questions/26082122/cannot-duplicate-handle-of-file-with-known-path-but-have-its-handle-entry-info see the comments for linked code. – Noitidart Nov 26 '14 at 20:46

6 Answers6

5

Process Monitor

Process Explorer

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
3

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

2

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

Kapil
  • 9,469
  • 10
  • 40
  • 53
  • This is actually on my "must-have" list for Windows... I find that whether I want to or not, I end up installing it well within a week of installing Windows. – Matthew Scharley Feb 01 '09 at 09:25
1

For unix you can use fuser:

lnx0:i386_linux26> fuser -v a.cpp

                     USER        PID ACCESS COMMAND
a.cpp                nabcdefg    3952 f....  less
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
1

It's the lsof command under Linux systems.

Drejc
  • 14,196
  • 16
  • 71
  • 106
-1

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.

Gustavo Rubio
  • 10,209
  • 8
  • 39
  • 57