I am enumerating processes to find if excel.exe
is running (for example).
I get a lot of Win32Exception
from system services and such.
Process[] pps = Process.GetProcesses();
foreach (var process in pps)
{
string module = null;
try
{
module = process.MainModule?.FileName;
}
catch (Win32Exception)
{
continue;
}
which make enumeration to run 500ms instead of 10ms.
Is there a way to figure if a process has main module without triggering the exception? Or any other way to find process exe path?
CodePudding user response:
This exception occurs when you're trying to do something which your OS doesn't allow. You can check the NativeErrorCode
property to see more details about the exception.
You can find a solution here to deal with that issue.
As mentioned by @steeeve in comment, you can use GetProcessByName
, if performance is the only criteria for you.