我正在使用以下代码。
ServiceController MyController = new ServiceController();
MyController.MachineName = server_txt.Text.Trim();
MyController.ServiceName = "Service1";
string msg = MyController.Status.ToString();
Label1.Text = msg;
此代码适用于我可以访问的网络计算机。如何更改此设置以使其适用于使用凭据的不同域中的系统?
最佳答案
如果您使用 WMI,您可以在“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# - 使用 C# 检查在远程计算机中运行的服务的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1335065/