为了更新我自己编写的Windows服务 ,我编写了一个停止服务的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);
在停止服务工作时,start方法产生以下错误:
Cannot start service FOVEA Service Debug on computer '.'. at System.ServiceProcess.ServiceController.Start(String[] args) at System.ServiceProcess.ServiceController.Start()
以pipe理员身份运行我的更新程序没有区别。
所以我试图通过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
该服务为本地login用户(pipe理员)安装,但我不明白为什么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)); }
进行一些异常处理以避免错误。