[英](Re)Starting a Windows Service from out of my C# code does not work
为了更新自己编写的Windows Service ,我编写了一个C#程序来停止该服务,复制新的DLL,然后重新启动它。
我的第一种方法是:
ServiceController service = new ServiceController(serviceName);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
// (...) copy the dll files
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
在停止服务工作时,启动方法产生以下错误:
Cannot start service FOVEA Service Debug on computer '.'.
at System.ServiceProcess.ServiceController.Start(String[] args) at
System.ServiceProcess.ServiceController.Start()
以管理员身份运行我的更新程序没有任何区别。
因此,我尝试通过net start/stop service
在命令行中net start/stop service
,该net start/stop service
运行良好,并且将C#程序更改为以下内容:
Process p = Process.Start("net", "stop \"" + serviceName + "\"");
p.WaitForExit();
p = Process.Start("net", "start \"" + serviceName + "\"");
p.WaitForExit();
和以前一样,停止服务也可以。 net start service
的呼叫却失败了
The service is not responding to the control function
more help is available by typing NET HELPMSG 2186
已为本地登录用户(管理员)安装了该服务,但我看不到为什么net start
可从命令行运行,但并非超出代码范围。 我还尝试了以下方法:
NETWORK SERVICE
我的更新程序设置完全权限 p.StartInfo.Verb = "runas"
开始该过程 p.StartInfo.WorkingDirectory
更改为Environment.SystemDirectory
p.MachineName
更改为Environment.MachineName
但是似乎没有任何影响。
有什么想法如何得到我想要的吗? 在此先感谢您的帮助。
尝试这种方式:
ServiceController sc = new ServiceController(svr_name);
if (sc.Status != ServiceControllerStatus.Stopped)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30));
}
放置一些异常处理以避免错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.