Check status of services that run in a remote computer using C#

后端 未结 2 1687
野的像风
野的像风 2020-12-05 03:16

I\'m using the following code.

ServiceController MyController = new ServiceController();
MyController.MachineName = server_txt.Text.Trim();
MyController.Serv         


        
2条回答
  •  南笙
    南笙 (楼主)
    2020-12-05 03:40

    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 }
    
    }
    

提交回复
热议问题