简体   繁体   中英

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: c:\\Windows\\system32\\qwinsta.exe

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

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: c:\\Windows\\system32\\qwinsta.exe

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

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: c:\\Windows\\system32\\qwinsta.exe

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

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: c:\\Windows\\system32\\qwinsta.exe

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

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: c:\\Windows\\system32\\qwinsta.exe

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

Here is my code sample which works. I am making the code to use x86 process instead of x64, as W10 is x64.

IntPtr ptr = new IntPtr();
Wow64DisableWow64FsRedirection(ref ptr);
var p = Process.Start(
         new ProcessStartInfo("qwinsta", $"/server:")
         {
             CreateNoWindow = true,
             UseShellExecute = false,
             RedirectStandardError = true,
             RedirectStandardOutput = true,
             WorkingDirectory = Environment.CurrentDirectory
         }
     );

p.WaitForExit();
string output = p.StandardOutput.ReadToEnd().TrimEnd();
string errorInfoIfAny = p.StandardError.ReadToEnd().TrimEnd();

Wow64RevertWow64FsRedirection(ptr);

Add this two import methods.

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);

样本输出

For system-level Applications

    [DllImport("Wtsapi32.dll")]
    public static extern bool WTSQuerySessionInformationW(
          IntPtr hServer,
          int SessionId,
          int WTSInfoClass,
          out IntPtr ppBuffer,
          out IntPtr pBytesReturned);

public static void AddArray<xArray>(ref xArray[] array, dynamic deger)
    {
        try
        {
            int diziboyu;
            if (array == null)
            {
                diziboyu = 0;
            }
            else
                diziboyu = array.Length;
            diziboyu++;
            System.Array.Resize(ref array, diziboyu);
            array[diziboyu - 1] = deger;
        }
        catch (Exception)
        {

        }
    }

public static string[] Online_userNames = null;

Process[] pname = Process.GetProcessesByName("explorer");
foreach (Process proc in pname)
            {
                IntPtr AnswerBytes;
                IntPtr AnswerCount;
                WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE,
                                                    proc.SessionId,
                                                    WTS_UserName,
                                                    out AnswerBytes,
                                                    out AnswerCount);

                AddArray(ref Online_userNames, Marshal.PtrToStringUni(AnswerBytes));
            }

            pname = null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM