Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# - check if program is running for current user

Tags:

c#

I need to check whether a program (xyz.exe) is running, but only for the current user. Whatever method is used cannot require elevated rights, and has to run fast (so WMI is out).

Process.GetProcessesByName("xyz") returns results for "xyz" for all logged in users... but I only care about the current user.

Ideas?

like image 565
ltwally Avatar asked Feb 11 '15 18:02

ltwally


1 Answers

Use the current process SessionId to filter the list of processes:

    public static bool IsProcessRunningSameSession(string processName)
    {
        var currentSessionID = Process.GetCurrentProcess().SessionId;
        return Process.GetProcessesByName(processName).Where(p => p.SessionId == currentSessionID).Any();
    }
like image 197
Geoff Avatar answered Oct 23 '22 12:10

Geoff