简体   繁体   中英

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

I'm trying to write a faster user switching app for Windows. Win+L and selecting users is very cumbersome. If I start Task Manager as administrator, it shows active users and I can select one and "Connect" (if I enter their password).

How do I get the list of all users (or all active users)?

I'm using C# (Visual Studio Express).

If you'd rather not deal with the P/Invokes, you can use Cassia , which wraps the ugly for you:

using Cassia;

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

我会尝试WTSEnumerateSessions来获取所有可用的会话。

You can also use NetWkstaUserEnum to see all users currently logged in; it's not really necessarily better, but it's another option. It has one advantage that it will work on older systems which don't support terminal services, but that's probably not an issue if you're using C#. :)

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