15

I'm writing a Windows Service application which listens for connections and performs certain tasks as instructed from a different application running on another computer on the network.

One of the tasks ensures no user is currently logged on, locks the workstation, delete some files, and then restarts the system. I considered using this solution to look through the list of running processes and check the user names, determining if no user is logged on by matchhing the user names against SYSTEM, NETWORK, etc. I realized I have PostgreSQL running which uses a user account named postgres so that wouldn't work. Checking if explorer.exe is running also wouldn't work because explorer sometmes crashes, or I sometimes end the process myself and restart it.

What would be a good way of determining that NO user is logged on to a workstation using C#?

Community
  • 1
  • 1
Zahymaka
  • 6,523
  • 7
  • 31
  • 37
  • 1
    Thanks a lot everyone. I'm not very good at using WMI and after hitting error after error, decided to go for flodin's answer. Here's a sample usage for anyone else interested: http://www.pinvoke.net/default.aspx/wtsapi32/WTSEnumerateSessions.html – Zahymaka Feb 15 '09 at 21:37

5 Answers5

16

Use WTSGetActiveConsoleSessionId() to determine whether anybody is logged on locally. Use WTSEnumerateSessions() to determine if there is any session at all (including remote terminal services sessions).

onnodb
  • 5,241
  • 1
  • 32
  • 41
flodin
  • 5,215
  • 4
  • 26
  • 39
8

Another option, if you don't want to deal with the P/Invokes: use Cassia.

using Cassia;

public static bool IsSomeoneLoggedOn(string server)
{
    foreach (ITerminalServicesSession session in new TerminalServicesManager().GetSessions(server))
    {
        if (!string.IsNullOrEmpty(session.UserName))
        {
            return true;
        }
    }
    return false;
}
Dan Ports
  • 1,421
  • 9
  • 7
  • 1
    what is `server` in this context? – Neil Galiaskarov Jul 06 '18 at 03:43
  • @NeilGaliaskarov Its obsolete now. Tested it with `new TerminalServicesManager().GetSessions()` in a domain user session and got my username via `session.UserName`. Otherwise you could check out `new TerminalServicesManager().GetLocalServer()` (I didn't test this one) – Icad May 04 '22 at 16:44
7

You tried to check whether explorer.exe is running or not. Why not go for the winlogon.exe process?

public bool isLoggedOn()
{
    Process[] pname = Process.GetProcessesByName("winlogon");
    if (pname.Length == 0)
        return false;
    else
        return true;
}
libjup
  • 4,019
  • 2
  • 18
  • 23
3

The CodeProject article "Using the Local Security Authority to Enumerate User Sessions in .NET" might be what you are looking for. The code enumerates users and can identify which users (if any) are interactive (i.e., which users are real people).

snarf
  • 2,684
  • 1
  • 23
  • 26
2

You could use WMI

select UserName from Win32_ComputerSystem
Alex Reitbort
  • 13,504
  • 1
  • 40
  • 61
  • 1
    If someone down-votes this, please provide an explanation as to why. Is the answer wrong? Or are there just better ways to do this? – DOK Feb 15 '09 at 19:33
  • 1
    I don't like this answer, not down-voting, however. – unixman83 Sep 16 '11 at 22:43
  • WMI is expensive in terms of cycles and can be slow, but for a one-off query it's not an issue. If you are using WMI in a loop, it's expensive and you'll see things like wmiprvse.exe taking up CPU cycles – Jon R Mar 01 '21 at 17:12