If I read or write to a file and receive a 33 or 32 error, I would like to log a message containing the name of the other process(es) that have the file opened. There must be a Win32 API I could use to get this info. Process Explorer displays it. Of course, Process Explorer also has info about all processes in memory. I'd prefer to find the culprit without interrogating all processes.
Asked
Active
Viewed 5,549 times
13
-
do you get final solution using only C# code ? not tools like handle or another – Kiquenet Sep 30 '20 at 04:15
2 Answers
2
As of Windows Vista, the Restart Manager can be used to determine which process(es) have a file open.
This page has sample code: https://devblogs.microsoft.com/oldnewthing/20120217-00/?p=8283
From that page, the steps (for the sample code) are the following:
- Create a Restart Manager session.
- Add a file resource to the session.
- Ask for a list of all processes affected by that resource.
- Print some information about each process.
- Close the session.
Here is the documentation for the Restart Manager: https://learn.microsoft.com/en-us/windows/win32/rstmgr/restart-manager-portal

David
- 76
- 7
1
The Handle program from SysInternals does this as well, but I'm not sure how. I don't think there is a single documented API to do this. I think you have to iterate over the processes, enumerate each of the handles in the process, and then determine the filename associated with that handle.

Nick
- 5,875
- 1
- 27
- 38