I need to check if another process is running based only on the name of the EXE file.
Currently, I get a list of processes and then request a property MainModule.FileName, however some processes throw Win32Exception"Unable to list process modules" when accessing a property MainModule. I am currently filtering into a "safe list", catching these access exceptions, this way:
List<Process> processes = new List<Process>(Process.GetProcesses());
List<Process> safeProcesses = new List<Process>();
foreach (Process p in processes)
{
try
{
ProcessModule pm = p.MainModule;
safeProcesses.Add(p);
}
catch { }
}
Needless to say, I really don't like to use exceptions like this.
Is there a better way to get a list of processes for which I can access a property MainModule, or even a better method to check if any process from a given file has been called?