3

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).

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
djb
  • 4,930
  • 1
  • 34
  • 37

3 Answers3

4

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);
    }
}
Dan Ports
  • 1,421
  • 9
  • 7
1

I'd try WTSEnumerateSessions to get all available sessions.

fhe
  • 6,099
  • 1
  • 41
  • 44
  • The following shows how to invoke such API from C#: http://www.pinvoke.net/default.aspx/wtsapi32/WTSEnumerateSessions.html (Thanks to Philip Fourie http://stackoverflow.com/users/11123/philip-fourie) – djb Oct 12 '08 at 20:44
1

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#. :)

Nick
  • 6,808
  • 1
  • 22
  • 34
  • 1
    I tried this (in a C++ app - easier to call it) and it does not return other logged in users, even when I run the app via 'Run as Administrator'. I tried both values, 0 and 1, for dwLevel. – djb Oct 12 '08 at 00:11