標籤:
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
c#訪問遠端電腦的Windows Service狀態