C# 检查当前是否没有用户登录到 Windows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/551412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:50:06  来源:igfitidea点击:

Check if no user is currently logged on to Windows

c#.netwindows-services

提问by Zahymaka

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.

我正在编写一个 Windows 服务应用程序,它侦听连接并按照网络上另一台计算机上运行的不同应用程序的指示执行某些任务。

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

其中一项任务确保当前没有用户登录、锁定工作站、删除一些文件,然后重新启动系统。我考虑使用此解决方案来查看正在运行的进程列表并检查用户名,通过将用户名与 SYSTEM、NETWORK 等进行匹配来确定是否没有用户登录。我意识到我正在运行 PostgreSQL,它使用名为postgres 这样就行不通了。检查 explorer.exe 是否正在运行也不起作用,因为 explorer sometmes 崩溃,或者我有时自己结束进程并重新启动它。

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

确定没有用户使用 C# 登录到工作站的好方法是什么?

采纳答案by 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).

使用WTSGetActiveConsoleSessionId()以确定是否有人在本地登录。使用WTSEnumerateSessions()以确定是否有在所有的任何会话(包括远程终端服务会话)。

回答by Alex Reitbort

You could use WMI

你可以使用 WMI

select UserName from Win32_ComputerSystem

回答by snarf

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

CodeProject 文章“ Using the Local Security Authority to Enumerate User Sessions in .NET”可能正是您要找的。该代码枚举用户并可以识别哪些用户(如果有)是交互的(即,哪些用户是真人)。

回答by Dan Ports

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

另一种选择,如果您不想处理 P/Invokes:使用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;
}

回答by libjup

You tried to check whether explorer.exeis running or not. Why not go for the winlogon.exeprocess?

您试图检查是否explorer.exe正在运行。为什么不走winlogon.exe流程呢?

public bool isLoggedOn()
{
    Process[] pname = Process.GetProcessesByName("winlogon");
    if (pname.Length == 0)
        return false;
    else
        return true;
}