无法在计算机的“IP地址”上打开服务控制管理器。此操作可能需要其他权限

时间:2014-01-20 21:06:08

标签: c# windows security networking service

我是C#的新手,我收到了这个错误:

  

无法在计算机'172.168.1.106'上打开服务控制管理器。这个   操作可能需要其他权限。

当我尝试启动/停止安装在属于我的计算机的同一网络的另一台计算机中的服务时。那么有人有任何准时的解决方案吗?任何教程?该服务作为网络服务安装在另一台计算机中。

这是我的代码的外观(必不可少的部分):

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

1 个答案:

答案 0 :(得分:1)

首先,您需要模拟拥有服务的计算机。要做到这一点 您可以使用下面的代码段。要使用下面的代码,您应该参考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();