Windows services C#检查windows服务状态

Windows services C#检查windows服务状态,windows-services,Windows Services,我正在使用以下代码检查我的windows服务状态: ServiceController sc = new ServiceController("service name", "service location"); 在服务位置中,我将输入我的服务所在的服务器名称。 在我的测试环境中,我有服务和门户(IIS),我在同一台服务器上检查我的服务,它工作正常,但在我的生产环境中,该服务位于与门户IIS不同的服务器上 我的代码无法检查状态。我肯定这是一个权限问题,但我尝试了这么多使它工作,但没有用 我

我正在使用以下代码检查我的windows服务状态:

 ServiceController sc = new ServiceController("service name", "service location");
在服务位置中,我将输入我的服务所在的服务器名称。 在我的测试环境中,我有服务和门户(IIS),我在同一台服务器上检查我的服务,它工作正常,但在我的生产环境中,该服务位于与门户IIS不同的服务器上

我的代码无法检查状态。我肯定这是一个权限问题,但我尝试了这么多使它工作,但没有用


我的问题是:给什么“用户”或“机器名”什么“许可类型”?请帮助。

您的IIS门户是否模拟?看来是的。因此,你陷入了“两跳”。您无法“修复”此问题。您必须请求域管理员为IIS应用程序启用和配置受约束的委派


另一种方法是不在IIS中模拟。在这种情况下,您需要向门户应用程序池授予适当的权限。中所需的权限。阅读更多信息。显然,应用程序池必须是域帐户,并且IIS和目标服务主机应该位于同一个域中,或者位于具有信任关系的域中

无论如何,多亏了大家,我通过WMI使用了另一种方式,它起了作用:

ConnectionOptions op = new ConnectionOptions();
op.Username = "";
op.Password = "";
ManagementScope scope = new ManagementScope(SVClocation+@"\root\cimv2", op);
scope.Connect();
ManagementPath path = new ManagementPath("Win32_Service");
ManagementClass services = new ManagementClass(scope, path, null);
foreach (ManagementObject service in services.GetInstances())
{
    if (service.GetPropertyValue("Name").ToString().ToLower().Equals("serviceName"))
    {
        if (service.GetPropertyValue("State").ToString().ToLower().Equals("running"))
        {
            //do something
        }
        else
        {
            //do something
        }
    }
}
看见