10

Is there a way in .Net to find out exactly which process has locked a file?

EDIT: I'm doing this because I want to let my user know that they can't modify/open the file, because at the moment, another program they're using (such as Excel) has it open. Hopefully, this helps.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Irwin
  • 12,551
  • 11
  • 67
  • 97
  • 1
    The information would be useless, because by the time you do something with it, it could no longer be accurate. – Josh Stodola Mar 19 '10 at 18:45
  • 7
    Really Josh? You sure about that... – HasaniH Mar 19 '10 at 18:50
  • @SpaceghostAli Files are always locked indefinitely, no? – Josh Stodola Mar 19 '10 at 18:56
  • Related: http://stackoverflow.com/questions/1304/how-to-check-for-file-lock-in-c – Josh Stodola Mar 19 '10 at 18:58
  • 1
    @Josh Stodola The information would not be useless, I gave this question +1 because I have a use case in a production system where this info would be very handy – HasaniH Mar 19 '10 at 19:04
  • It's very useful in some circumstances. My users loved the "This drawing is locked by " message I put in. – Loren Pechtel Mar 19 '10 at 19:04
  • @SpaceghostAli OK, I see, I can appreciate that the user might find this information useful. But the bottom line is that programmatically tapping into to such information is never going to be 100% precise. So, I guess it's up to you to decide if you want to occasionally misinform your users. – Josh Stodola Mar 19 '10 at 19:11

3 Answers3

6

The short answer to this is no.

However, the long answer is that there are various API calls and WMI methods that you can use to find this information out, but don't expect it to be quick and simple.

If you want to use API calls, take a look at the NtQuerySystemInformation function with the SYSTEM_PROCESS_INFORMATION parameter. This is one of those lovely "undocumented" methods that comes with the wonderful disclaimer:

NtQuerySystemInformation may be altered or unavailable in future versions of Windows. Applications should use the alternate functions listed in this topic.

So I would suggest avoiding that in favour of using WMI.

You can use the WMI Win32_Process class to enumerate all processes currently running on the machine, and then enumerate all handles each process is holding until you find the file you are looking for. Unfortunatly there is no simple way to go "hey, which process is locking this file", it only works the other way round you have to search down the process list until you find the one that is locking the file you are interested in.

I'd recommend a nice little article on CodeProject titled How To: (Almost) Everything In WMI via C# Part 2: Processes. (Part 1 is also a good read if you like that kind of thing)

Simon P Stevens
  • 27,303
  • 5
  • 81
  • 107
1

Well... it's not exactly a .NET way of doing it... but assuming you just want to find out using an easy utility, check out Handle from SysInternals.

Nick
  • 5,875
  • 1
  • 27
  • 38
  • 2
    I think Irwin is asking how to do it programmacitally, I don't think this really helps him with that. – Simon P Stevens Mar 19 '10 at 19:05
  • @Simon Well, technically, he could trap the exception and then programmatically invoke this command line tool to get the process(es) currently using the file. Far from ideal, yes, but possible! – Josh Stodola Mar 19 '10 at 19:14
0

Looks like somebody has figured this one out (and you may learn some French as well :) )

http://www.axcis.com.au/bb/viewtopic.php?p=505

holtavolt
  • 4,378
  • 1
  • 26
  • 40