1 static void Main(string[] args) 2 { 3 ConnectionOptions op = new ConnectionOptions(); // 登陆远程计算机的远程, 4 op.Username = "Domain\\Domainuser"; 5 op.Password = "password"; 6 // 该处不能使用ip必须是"计算机名称.域名\root\cimv2" 7 ManagementScope scope = new ManagementScope(@"\\Servername.Domain\root\cimv2", op); 8 scope.Connect(); 9 ManagementPath path = new ManagementPath("Win32_Service"); 10 using(ManagementClass services = new ManagementClass(scope, path, null)) 11 { 12 foreach (ManagementObject service in services.GetInstances()) 13 { 14 Console.WriteLine("{0}:{1}",service.GetPropertyValue("Name").ToString(),service.GetPropertyValue("State").ToString()); 15 } 16 } 17 18 Console.ReadKey(); 19 }
运行结果,果然不负众望。
参考:
1,)http://www.codeproject.com/Articles/36268/Monitor-and-Manage-Services-on-Remote-Machines
2,)http://www.codeitive.com/0HJgVkjVeq/check-status-of-services-that-run-in-a-remote-computer-using-c.html
时间: 2025-01-09 04:31:24