c# - 无法在计算机 'ip address' 上打开服务控制管理器。此操作可能需要其他权限

标签 c# windows security networking service

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

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

最佳答案

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

关于c# - 无法在计算机 'ip address' 上打开服务控制管理器。此操作可能需要其他权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21244138/

相关文章:

c# - 激活函数、初始化函数等对人脸检测神经网络的影响

windows - 即使启用 Windows 长路径,ALINK 错误 1065

c# - .Net 核心命令行应用 | Process.Start() 在某些机器上运行,但在其他机器上不运行

php - 对 cookie 进行编码,使其不会被欺骗或读取等

php - 您在开源 cms 安装中添加了哪些额外的安全措施?

c# - Window.Open 替换失败

c# - 覆盖 == 似乎在继承类中不起作用

c# - 文件上传安全问题

c# - C# 中的 Windows 服务无法启动

Java 7 网络启动