I create an application that records the user's activity history on the computer. At the moment I need to find out the path to the file (for example с:\documents\FileName.docx
) that is open in the Word for example. I just got to know the path to the EXE file. I have process id, but in ManagementObject
I did not find any information about executable file path. How can I do this? Below method how I take .exe file path by process id.
public static string GetMainModuleFilepath(int processId)
{
string wmiQueryString = "SELECT ProcessId, ExecutablePath FROM Win32_Process WHERE ProcessId = " + processId;
using (var searcher = new ManagementObjectSearcher(wmiQueryString))
{
using (var results = searcher.Get())
{
ManagementObject mo = results.Cast<ManagementObject>().FirstOrDefault();
return (string)mo?["ExecutablePath"];
}
}
}