.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Find open file handles for a process.
 

Find open file handles for a process.

I need to discover what file handles a process has open. The best example is what system internals ProcessExplorer does.
However they use NtQuerySystemInformation which it a C++ call.

While I'm not against wrapping the call I figure there must be a direct C# call.

The closest c# option I can find is WMI calls.
Win32_Process will tell me what processes I'm running but it won't tell me what file handles I have open.

Question:
1) Is there a WMI call that can tell me what File Handles are open? Either for a given process or at least what PID is holding the file handle?
2) Is there any other call I can do to copmlete this task?

asdf989
Check this project. In case it isn't clear, it uses private kernel structures that are different in each version of Windows. Relying on this in production software is very unwise.

Hans Passant.
nobugz

Ya, I've actually already startedusing this particular project. It covers what I need but it's not in C# and as you point out, its never going to be forwards compatible.

asdf989
Hello

NtQuerySystemInformation is not recommended to use in applications, so I do not suggest it even though you may be able to P/Invoke the API from C#.

I'm not aware of a WMI class that can tell what file handles are open. May I know the business requirement behind the request of enumerating open file handles for a process? If you want to find out who has a file open, you may propably try the restart manager API which is available in Windows Vista and later operating systems.

http://msdn.microsoft.com/en-us/library/aa373524(VS.85).aspx
The primary reason software installation and updates require a system restart is that some of the files that are being updated are currently being used by a running application or service. Restart Manager enables all but the critical applications and services to be shut down and restarted . This frees the files that are in use and allows installation operations to complete. It can also eliminate or reduce the number of system restarts that are required to complete an installation or update.

Another idea is to redirect stdIO of handle.exe utility
http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx
and parseits output to get the opened files of a process. However, third party applications are not allowed to distribute the tool:

http://technet.microsoft.com/en-us/sysinternals/bb847944.aspx
Q: May I distribute Sysinternals utilities in my software, on my website, or with my magazine?
A: No. We are not offering any distribution licenses, even if the 3rd party is distributing them for free. We encourage people to download the utilities from our download center where they can be assured to get the most recent version of the utility.

Regards,
Jialiang Ge
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Jialiang Ge [MSFT]

You can use google to search for other answers

Custom Search

More Threads

• error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
• Suspending a computer with a screen saver from the login screen
• Saving Certificate to "My" store
• What is the advantage to use the standard event handler?
• AppDomain + Plugin Event => TargetInvocationException & FileNotFoundException
• Bitmap class - ImageFormat errors
• Mulitple Registry keys
• Interaction between separated Windows Application.
• Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on.
• How do I translate a number based on the current culture, and assign that translated number to a string variable?