Check if no user is currently logged on to Windows

前端 未结 5 729
野的像风
野的像风 2020-12-24 08:42

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

相关标签:
5条回答
  • 2020-12-24 09:04

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

    0 讨论(0)
  • 2020-12-24 09:04

    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;
    }
    
    0 讨论(0)
  • 2020-12-24 09:09

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

    0 讨论(0)
  • 2020-12-24 09:16

    You could use WMI

    select UserName from Win32_ComputerSystem
    
    0 讨论(0)
  • 2020-12-24 09:27

    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;
    }
    
    0 讨论(0)
提交回复
热议问题