tags:

views:

448

answers:

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

+1  A: 

I'd try WTSEnumerateSessions to get all available sessions.

fhe
The correct URL is http://msdn.microsoft.com/en-us/library/aa383833(VS.85).aspx
djb
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
A: 

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
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
+1  A: 

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