检查服务的状态,使用C#在远程计算机上运行 [英] Check status of services that run in a remote computer using C#

查看:198
本文介绍了检查服务的状态,使用C#在远程计算机上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的代码

ServiceController MyController = new ServiceController();
MyController.MachineName = server_txt.Text.Trim();
MyController.ServiceName = "Service1";

string msg = MyController.Status.ToString();
Label1.Text = msg;

此代码工作正常,为网络计算机在那里我有机会。如何改变这种因此它适用于使用证书在不同领域的系统?

This code works fine for network computers where I have access. How to change this so it works for the systems in different domains using credentials?

请帮我。

推荐答案

如果您使用WMI,您可以在ConnectionOptions设置凭据。

If you use WMI, you can set the credentials in 'ConnectionOptions'.

ConnectionOptions op = new ConnectionOptions();
op.Username = "Domain\\Domainuser";
op.Password = "password";
ManagementScope scope = new ManagementScope(@"\\Servername.Domain\root\cimv2", op);
scope.Connect();
ManagementPath path = new ManagementPath("Win32_Service");
ManagementClass services;
services = new ManagementClass(scope, path, null);

foreach (ManagementObject service in services.GetInstances())
{

if (service.GetPropertyValue("State").ToString().ToLower().Equals("running"))
{ // Do something }

}

这篇关于检查服务的状态,使用C#在远程计算机上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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