我正在Windows Server 2012 R2上设置Windows服务。当我将服务安装到我的计算机(win10 Entr。)时,它可以正常工作。我可以从与服务一起安装的UI中启动和停止服务。然而, 如果我将服务和UI安装到Windows Server 2012 R2,则我的日志向我显示它无法从UI停止服务。在用户界面中,当我单击“停止”按钮时,下面的代码起作用。
ServiceController sc = new ServiceController();
sc.ServiceName = "xxService";
if (sc.Status == ServiceControllerStatus.Running)
{
try
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
return true;
}
catch (InvalidOperationException e)
{
return false;
}
}
我想知道我是否必须为服务器端处理一些事情。上面的代码似乎无法停止该服务。
答案 0 :(得分:0)
非常抱歉,抽出宝贵的时间回答这样的问题。我只是以管理员身份运行UI,然后就可以了。