Reputation: 13367
Here is method which must return processes by path to executeble file. But when i try to call this method i have an exception Win32Exception "Access is denied". So how to do it right?
private static List<Process> GetProcessByFilename(string filename)
{
List<Process> processes = new List<Process>();
foreach (var process in Process.GetProcesses())
{
if (process.MainModule.FileName == filename)
{
processes.Add(process);
}
}
return processes;
}
Upvotes: 2
Views: 2684
Reputation: 61007
Where exactly is this exception thrown? Firstly, you aren't allowed to query all Win32 processes paths, some don't have one and lastly you might be running with insufficient access privileges.
To know which applies to your case I need to know where in your code path you get the exception as well as what process (if not all) throws the exception.
Upvotes: 1
Reputation: 22104
As the message says, you have "Access denied" issue. It's possible that your Windows Credential doesn't have necessary privileges to make that required Win32 call..
Upvotes: 1
Reputation: 1588
You will get Win32Exception
when trying to get MainModule
of core system processes (see comments on MSDN). You should handle that.
Upvotes: 4