查找该帐户的服务被设置为QUOT&;登录为"设置为、帐户、QUOT

2023-09-03 05:11:02 作者:俄的心伱卜懂乜卜想懂

如何找出用户帐户(本地系统/用户等 )服务被设置为根据(登录为)运行?

How to find out the user account (Local System/User etc) a service is set to run under ("Log On As")?

与此不同类似的问题可以牛逼从服务本身和服务中运行可能没有运行。

Unlike this similar question this code can't run from within the service itself and the service may not be running.

的System.ServiceProcess.ServiceController类有用于获取状态,但不是登录为用户有用的方法。

The System.ServiceProcess.ServiceController class has useful methods for getting the status but not the "Log On As" user.

推荐答案

这是我所知道的,我发现它环顾四周,测试它,它工作的唯一途径。请确保您使用的服务名称不是它的显示名称,则还需要添加一个引用 System.Management

This is the only way I know of, I found it looking around and tested it, it works. Make sure you use the Service Name not it's Display Name, you will also need to add a reference to System.Management

string serviceName = "aspnet_state";

SelectQuery query = new System.Management.SelectQuery(string.Format(
    "select name, startname from Win32_Service where name = '{0}'", serviceName));
using (ManagementObjectSearcher searcher =
    new System.Management.ManagementObjectSearcher(query))
{
    foreach (ManagementObject service in searcher.Get())
    {
        Console.WriteLine(string.Format(
            "Name: {0} - Logon : {1} ", service["Name"], service["startname"]));
    }
}