How to get a list of local Windows users who are logged in?

I am trying to write a faster user switching application for Windows. Win + L and user selection are very cumbersome. If I run the task manager as an administrator, it shows the active users, and I can choose one and "Connect" (if I enter their password).

How to get a list of all users (or all active users)?

I am using C # (Visual Studio Express).

+3
source share
3 answers

I would try WTSEnumerateSessions to get all available sessions.

+1
source

P/Invokes, Cassia, :

using Cassia;

foreach (ITerminalServicesSession session in new TerminalServicesManager().GetSessions())
{
    if (!string.IsNullOrEmpty(session.UserName))
    {
        Console.WriteLine("Session {0} (User {1})", session.SessionId, session.UserName);
    }
}
+4

You can also use NetWkstaUserEnum to see all the users who are currently logged in; this is actually not necessarily better, but this is another option. It has one advantage that it will work on older systems that do not support terminal services, but this is probably not a problem if you use C #. :)

+1
source

Source: https://habr.com/ru/post/1698173/


All Articles