我是 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/