c# - 使用 C# 检查在远程计算机中运行的服务的状态

标签 c# windows windows-services remote-access

我正在使用以下代码。

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/

相关文章:

c++ - 拦截套接字函数(Windows)

c# - 尝试访问映射驱动器时,服务中的文件复制失败

c++ - 应该在 ServiceMain 上创建一个新线程?

c# - 从 Windows 服务播放 wave 文件 (C#)

c# - 暂停执行嵌入式 IronPython

c# - 如何在asp.net core razor页面中访问部分页面中的 session ?

c# - c# 中 My.Computer 的等价物是什么

c# - 带动态元素的 WPF 启动屏幕。如何?

Python扩展DLL安装

database - 共享文件数据库建议