c# - 如何从窗口服务中获取登录用户和机器名? [英] how to get logged in user and machine name from window service in c#?

查看:30
本文介绍了c# - 如何从窗口服务中获取登录用户和机器名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#中如何从window服务中获取登录用户(交互用户)和机器名.当我尝试 Environment 和其他类以登录用户名时,它只从窗口服务返回 NT AUTHORITY\SYSTEM.

How to get the logged in user (interactive user) and machine name from window service in c#. When i try Environment and other class to get logged in user name it just returns NT AUTHORITY\SYSTEM from window service.

推荐答案

该服务在 SYSTEM 帐户下执行,因此您在 Environment 类中看到的内容.机器名称应该不是问题(参见 Gmoliv 的评论).服务独立于可能登录的任何人执行:这是拥有它们的主要原因之一.

The service executes under the SYSTEM account, so that what you see in the Environment class. The machine name should not be a problem (see Gmoliv's comment). Services execute independently from whoever may be logged on: that's one of the main reasons to have them.

如果您想了解哪些用户(是的,可能不止一个)可能登录到您的计算机,则必须使用原始 Windows API 的 AFAIK.如果你真的想要这个,一种方法可能是遍历桌面打开指定桌面,获取关联的每个桌面的用户,以及 查找用户的帐户名(返回本地机器上的帐户名).如果您只希望用户可以在屏幕上看到某些内容,请使用 OpenInputDesktop 来获取一个句柄,而不是遍历所有这些.

If you want to find out what users (yes, there may be more than one) may be logged on to your computer, you'll have to use raw Windows API's AFAIK. If you really want this, one way could be to iterate through desktops, open the named desktop, get the associated user of each desktop, and look up the account name of the user (which returns the account name on the local machine). If you only want the user which may see something on screen, use OpenInputDesktop to get a handle instead of iterating through all of them.

请注意,这要求您的服务具有比平时更高的访问权限.我自己对这样的服务有点怀疑.

Note that this requires your service to have higher access rights than usual. I'd be a bit suspicious of such a service myself.

这篇关于c# - 如何从窗口服务中获取登录用户和机器名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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