Answer the question
In order to leave comments, you need to log in
How to get the paths of files and folders that a process is currently using in C#?
We need a method that could cyclically check which file or folder is currently being used by the process and return a list for further path analysis. Not so long ago I learned about PerformanceCounter , which partially simplified my life. But this question still weighs, according to the idea, the project itself is frozen. Since the concept of my application requires the solution of this issue. There is an example of what I want to achieve is the standard Windows Resource Monitor snap -in. There is a tab Disk > Disk Work, just select any process and you can see all the files and folders opened by this process, I also want to. Actually, that’s why I haven’t merged yet, since I assume that there is an API for this matter. net frameworkfat, not a single life of a programmer is enough to learn everything. Any solutions will be discussed.
It's better to show:
Answer the question
In order to leave comments, you need to log in
More or less like this
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern SafeProcessHandle OpenProcess(int access, bool inherit, int processId);
using (var processHandler = SafeProcessHandle.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, processId))
{
if (!processHandler.IsInvalid)
{
int needed = 0;
IntPtr[] hMods = new IntPtr[1024];
var strBuilder = new StringBuilder(1024);
var handle2 = new GCHandle();
try
{
if (!GetModuleFileNameEx(processHandler, IntPtr.Zero, strBuilder, (uint)(strBuilder.Capacity)))
return;
fnFileFound(strBuilder.ToString());
handle2 = GCHandle.Alloc(hMods, GCHandleType.Pinned);
var uiSize = (uint)(Marshal.SizeOf(typeof(IntPtr)) * (hMods.Length));
var flag = EnumProcessModulesEx(processHandler, handle2.AddrOfPinnedObject(), uiSize, ref needed, LIST_MODULES_ALL);
if (flag != 0)
{
var uiTotalNumberofModules = needed / (Marshal.SizeOf(typeof(IntPtr)));
for (var i = 0; i < uiTotalNumberofModules; i++)
{
GetModuleFileNameEx(processHandler, hMods[i], strBuilder, (uint)(strBuilder.Capacity));
fnFileFound(strBuilder.ToString());
if (!Active)
break;
}
}
}
finally
{
if (handle2.IsAllocated)
handle2.Free();
}
}
}
There is no API, you have to write your own driver. In order not to reinvent the wheel, it is easier to use the output of this utility.
https://docs.microsoft.com/en-us/sysinternals/down...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question