Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the list of open file handles by process in C#?

Tags:

c#

.net

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.

like image 928
Sam Saffron Avatar asked Oct 07 '08 03:10

Sam Saffron


People also ask

How do I list files open in a particular process?

Simply typing lsof will provide a list of all open files belonging to all active processes.

Which command is used to identify list of open files by a process?

Lsof is used on a file system to identify who is using any files on that file system. You can run lsof command on Linux filesystem and the output identifies the owner and process information for processes using the file as shown in the following output.

How can I see open file descriptors?

We can take advantage of the information exported under /proc and combine it with the ls command to list the open file descriptors of a process. In the /proc pseudo filesystem, we can find the open file descriptors under /proc/<pid>/fd/ where <pid> is the PID of a given process.

How do you check list of all open files by a user Sam '?

you can use lsof. this command is for find out what processes currently have the file open. if process opening the file, writing to it, and then closing it you can use auditing.


2 Answers

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.

like image 169
Sam Saffron Avatar answered Oct 23 '22 14:10

Sam Saffron


You can also run the command line app, Handle, by Mark Rusinovich, and parse the output.

like image 39
Mark Cidade Avatar answered Oct 23 '22 13:10

Mark Cidade