C# 远程检查服务状态

C# 远程检查服务状态,c#,winforms,C#,Winforms,我需要使用运行我的可执行文件(winform)的用户的凭据远程获取服务状态(正在运行,已停止) WMI是最好的方法吗 我需要这个查询才能在windows(7200320082012)上运行。有人能告诉我正确的方向吗 if (RemoteOSversion.Contains("Windows 7")) { var Windows7Query = xdoc.Elements("OS").Elements("Windows7"); foreach (var myServices in

我需要使用运行我的可执行文件(winform)的用户的凭据远程获取服务状态(正在运行,已停止)

WMI是最好的方法吗

我需要这个查询才能在windows(7200320082012)上运行。有人能告诉我正确的方向吗

if (RemoteOSversion.Contains("Windows 7"))
{
    var Windows7Query = xdoc.Elements("OS").Elements("Windows7");
    foreach (var myServices in Windows7Query)
    {
        var ServicesQuery = myServices.Elements("Services");
        foreach (var ServiceName in ServicesQuery)
        {
            var ServiceOutput = ServiceName.Value;
        }
    }
}
ServiceOutput是服务名称。我需要使用运行我的exe的用户的相同凭据检查此服务是否正在远程运行/停止是的,请使用WMI

WMI有一种称为WQL的查询语言,类似于SQL。您可以使用
System.Management
类在C#中执行这些操作

要使用WMI,您需要添加对
System.Management
程序集的引用。然后,您可以设置与WMI提供程序的连接(即
ManagementScope
),如下所示:

ConnectionOptions options = new ConnectionOptions();

// If we are connecting to a remote host and want to
// connect as a different user, we need to set some options
//options.Username = 
//options.Password =
//options.Authority = 
//options.EnablePrivileges = 

// If we are connecting to a remote host, we need to specify the hostname:
//string providerPath = @"\\Hostname\root\CIMv2";
string providerPath = @"root\CIMv2";

ManagementScope scope = new ManagementScope(providerPath, options);
scope.Connect();

您可以在上阅读更多有关WMI的信息,并在上解决问题。

使用WMI非常简单

var sc = new ServiceController(ServiceName, MachineName);
string result = sc.Status.ToString();

这不是使用WMI。
ServiceController
类是独立的,这种方式更好,因为它不依赖WMI所依赖的RPC服务-即使它正在运行,也可以报告为“不可用”。不鼓励只提供链接的答案,因为网站所有者存档/移动链接可能会破坏链接。此处应提供摘要,以供未来用户参考。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。-去吧@SaravananSachi!