How do I get the list of open file handles by process id in C#?
I'm interested in digging down and getting the file names as well.
Looking for the programmatic equivalent of what process explorer does.
Most likely this will require interop.
Considering adding a bounty on this, the implementation is nasty complicated.
Perhaps using command line tool:
OpenedFilesView v1.50 - View opened/locked files in your system (sharing violation issues)
http://www.nirsoft.net/utils/opened_files_view.html
You can P/INVOKE into the
NtQuerySystemInformation
function to query for all handles and then go from there. This Google groups discussion has details.Ouch this is going to be hard to do from managed code.
There is a sample on codeproject
Most of the stuff can be done in interop, but you need a driver to get the filename cause it lives in the kernel's address space. Process Explorer embeds the driver in its resources. Getting this all hooked up from C# and supporting 64bit as well as 32, is going to be a major headache.
Have a look at this file : http://vmccontroller.codeplex.com/SourceControl/changeset/view/47386#195318
And use:
Demo:
It has dependency over assembly
System.EnterpriseServices
Take a look at wj32's Process Hacker version 1, which can do what you asked, and more.
You can also run the command line app, Handle, by Mark Rusinovich, and parse the output.