c# – How to get active session/user on local machine (Windows 10)


Windows 10 on start automatically log-on multiple (local) users & programs.
So I might have some ‘ghost’ users logged in once only 1 user is ‘active’ at this time

I need to recognize in my app if user is really active (works in session), not being just logged by Windows in background.

Is there any way to do that in .NET ? (from non-admin account)
Googled a lot but count find anything reliable

PS. I found cmd line command (qwinsta) what seems doing what I need- but I dont know how to run it from C# code and read output
(Im receiving qwinsta not found)

Plz advise…

* EDIT *

Clarification: I dont need to find active user name (this is pretty easy)
Im looking to get info about all sessions on local machine and check which is currently active (see below qwinsta.exe output from command line).

Imagine all local users have my app in startup – app needs to do something, but only when user has ulocked session is doing something at this moment in front of computer (not being automatically logged in by this Windows-10 mechanism I personally hate…)

qwinsta.exe will do job for me but if I start this process from .NET it always says that qwinsta.exe cant be found. Even if I give full path:

 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 services                                    0  Disc
>console           carl                    1  Active
 rdp-tcp                                 65536  Listen


Here is my code sample which finally works (Thanks to @Eryk Sun) – maybe someone will find it usefull.
PS. A it was rightly pointed – I might should mention that my app was x86 but W10 is x64 …

public static bool IsUserActive(string userName) {
    Process p = new Process();
    p.StartInfo.UseShellExecute         = false;
    p.StartInfo.RedirectStandardOutput  = true;
    p.StartInfo.FileName                =  "c:\\Windows\\SysNative\\qwinsta.exe";
    p.StartInfo.Arguments               = userName;
    string output = p.StandardOutput.ReadToEnd();

    return output.Contains("  Active");

Answered By – Maciej

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published