检查当前是否没有用户登录Windows [英] Check if no user is currently logged on to Windows

查看:131
本文介绍了检查当前是否没有用户登录Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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

推荐答案

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

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

这篇关于检查当前是否没有用户登录Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆