Check if no user is currently logged on to Windows

半世苍凉 提交于 2019-12-02 17:41:53
flodin

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

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;
}

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;
}

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

You could use WMI

select UserName from Win32_ComputerSystem
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!