I have a piece of C# code that goes through all the running processes, finds a specific one, and gets its ExecutablePath. Problem is that, although it manages to find the wanted process, attempting to get the ExecutablePath returns null.
Now, I did some more experimenting on this, and it turns out that some processes the code gets a path for, others it returns null, and it appears to be arbitrary because I cannot find any correlation between the process and whether or not it returns the path.
The code is fine, but here it is anyways:
string path = null;
string processNameLowerCase = processName.ToLower() + ".exe";
ManagementClass managementClass = new ManagementClass("Win32_Process");
ManagementObjectCollection managementObjects = managementClass.GetInstances();
foreach (ManagementObject managementObject in managementObjects) {
string managedProcessNameLowerCase = ((string)managementObject["Name"]).ToLower();
if (managedProcessNameLowerCase.StartsWith(processNameLowerCase)) {
path = (string)managementObject["ExecutablePath"];
break;
}
}
All in all, what I want to know is how I can get the executable's path of the process I want.