2014-01-20 23 views
2

我在C#中非常新,我收到此錯誤:無法在計算機'ip地址'上打開服務控制管理器。此操作可能需要其他特權

Cannot open Service Control Manager on computer '172.168.1.106'. This operation might require other privileges.

當我嘗試啓動/停止安裝在屬於同一網絡的另一臺計算機服務我的電腦。那麼有沒有人有任何準時的解決方案呢?任何教程?該服務作爲網絡服務安裝在另一臺計算機上。

這是我的代碼看起來(基本部分):

ServiceController servicio = new ServiceController(nombre, "172.168.1.106"); 

public bool ReiniciarServicio() 
    { 
     try 
     { 
      if (servicio.Status == ServiceControllerStatus.Running) 
      { 
       servicio.Stop(); 
       servicio.WaitForStatus(ServiceControllerStatus.Stopped); 
       servicio.Start(); 
       servicio.WaitForStatus(ServiceControllerStatus.Running); 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
     catch (Exception) 
     { 
      throw; 
     } 
    } 
+0

這不太可能是因爲你在C#中這樣做。你有沒有嘗試在命令行上使用'sc'?您使用遠程機器上的管理員權限登錄的用戶帳戶?遠程機器的防火牆是否配置爲允許訪問? –

回答

2

首先,你需要模擬一個自己的服務的計算機。要做到這一點 你可以使用下面的代碼片段。爲了使用下面的代碼,你應該引用System.Management Assembly。

 using System.ServiceProcess; 
     . 
     . 
     .    

     ConnectionOptions options = new ConnectionOptions(); 
     options.Password = "password here"; 
     options.Username = "username"; 
     options.Impersonation = 
      System.Management.ImpersonationLevel.Impersonate; 

     // Make a connection to a remote computer. 
     // Replace the "FullComputerName" section of the 
     // string "\\\\FullComputerName\\root\\cimv2" with 
     // the full computer name or IP address of the 
     // remote computer. 
     ManagementScope scope = 
      new ManagementScope(
      "\\\\FullComputerName\\root\\cimv2", options); 
     scope.Connect(); 


     ServiceController sc = new ServiceController("ServiceName", "fullComputerName"); 
     sc.Start();`` 
相關問題