Neir0
Neir0

Reputation: 13367

Get processes by execution path

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

Answers (3)

John Leidegren
John Leidegren

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

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

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

Oleg Kolosov
Oleg Kolosov

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

Related Questions