How do I retrieve the username that a Windows service is running under?

后端 未结 6 1082
無奈伤痛
無奈伤痛 2020-12-09 09:44

Given a service name, I would like to retrieve the username that it runs under (i.e. the username shown in the \'Log On\' tab of a service\'s properties window). There doesn

6条回答
  •  暖寄归人
    2020-12-09 10:23

    This solution works fine for me:

        ManagementObject wmiService = new ManagementObject("Win32_Service.Name='" + this.ServiceName + "'");
        wmiService.Get();
        string user = wmiService["startname"].ToString();
    

提交回复
热议问题