我怎样才能在.NET中以编程方式重新启动Windows服务
我怎样才能在.NET中以编程方式重新启动Windows服务?
另外,当服务重启完成时,我需要做一个操作。
看看ServiceController类。
要执行服务重新启动时需要完成的操作,我想你应该在服务中自己做(如果是你自己的服务)。
如果您没有访问服务的来源,那么也许可以使用ServiceController
的WaitForStatus
方法。
本文使用ServiceController
类来编写启动,停止和重新启动Windows服务的方法; 这可能值得一看。
文章片段(“重新启动服务”方法):
public static void RestartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { int millisec1 = Environment.TickCount; TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); // count the rest of the timeout int millisec2 = Environment.TickCount; timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1)); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { // ... } }
ServiceController类使用的示例
private void RestartWindowsService(string serviceName) { ServiceController serviceController = new ServiceController(serviceName); try { if ((serviceController.Status.Equals(ServiceControllerStatus.Running)) || (serviceController.Status.Equals(ServiceControllerStatus.StartPending))) { serviceController.Stop(); } serviceController.WaitForStatus(ServiceControllerStatus.Stopped); serviceController.Start(); serviceController.WaitForStatus(ServiceControllerStatus.Running); } catch { ShowMsg(AppTexts.Information, AppTexts.SystematicError, MessageBox.Icon.WARNING); } }
你也可以调用net
命令来做到这一点。 例:
System.Diagnostics.Process.Start("net", "stop IISAdmin"); System.Diagnostics.Process.Start("net", "start IISAdmin");
怎么样
var theController = new System.ServiceProcess.ServiceController("IISAdmin"); theController.Stop(); theController.Start();
不要忘记将System.ServiceProcess.dll添加到您的项目中,以使其工作。
看到这篇文章 。
这是文章的一个片段。
//[QUICK CODE] FOR THE IMPATIENT using System; using System.Collections.Generic; using System.Text; // ADD "using System.ServiceProcess;" after you add the // Reference to the System.ServiceProcess in the solution Explorer using System.ServiceProcess; namespace Using_ServiceController{ class Program{ static void Main(string[] args){ ServiceController myService = new ServiceController(); myService.ServiceName = "ImapiService"; string svcStatus = myService.Status.ToString(); if (svcStatus == "Running"){ myService.Stop(); }else if(svcStatus == "Stopped"){ myService.Start(); }else{ myService.Stop(); } } } }
这个答案是基于@Donut Answer(这个问题的答案最多),但有一些修改。
- 每次使用后都要configuration
ServiceController
类,因为它实现了IDisposable
接口。 - 减less方法参数:不需要为每个方法传递
serviceName
参数,我们可以在构造函数中设置它,每个其他方法将使用该服务名称。
这也是更多的面向对象的友好。 - 处理捕获exception的方式,这个类可以作为组件使用。
- 从每个方法中删除
timeoutMilliseconds
参数。 - 添加两个新方法
StartOrRestart
和StopServiceIfRunning
,它们可以被认为是其他基本方法的包装,这些方法的目的只是为了避免exception,如注释中所述。
这是class级
public class WindowsServiceController { private string serviceName; public WindowsServiceController(string serviceName) { this.serviceName = serviceName; } // this method will throw an exception if the serivce is NOT in Running status. public void RestartService() { using (ServiceController service = new ServiceController(serviceName)) { try { service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running); } catch (Exception ex) { throw new Exception($"Can not restart the Windows Service {serviceName}", ex); } } } // this method will throw an exception if the serivce is NOT in Running status. public void StopService() { using (ServiceController service = new ServiceController(serviceName)) { try { service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped); } catch (Exception ex) { throw new Exception($"Can not Stop the Windows Service [{serviceName}]", ex); } } } // this method will throw an exception if the service is NOT in Stopped status. public void StartService() { using (ServiceController service = new ServiceController(serviceName)) { try { service.Start(); service.WaitForStatus(ServiceControllerStatus.Running); } catch (Exception ex) { throw new Exception($"Can not Start the Windows Service [{serviceName}]", ex); } } } // if service running then restart the serivce, if the service is stopped then start it. // this method will not throw an exception. public void StartOrRestart() { if (IsRunningStatus) RestartService(); else if (IsStoppedStatus) StartService(); } // stop the service if it is running, if it is already stopped then do nothing. // this method will not throw an exception if the service is in Stopped status. public void StopServiceIfRunning() { using (ServiceController service = new ServiceController(serviceName)) { try { if (!IsRunningStatus) return; service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped); } catch (Exception ex) { throw new Exception($"Can not Stop the Windows Service [{serviceName}]", ex); } } } public bool IsRunningStatus => Status == ServiceControllerStatus.Running; public bool IsStoppedStatus => Status == ServiceControllerStatus.Stopped; public ServiceControllerStatus Status { get { using (ServiceController service = new ServiceController(serviceName)) { return service.Status; } } } }
调用Environment.Exit
的错误代码大于0,这似乎是适当的,然后安装我们configuration服务重新启动时出错。
Environment.Exit(1);
我在我的服务中做了同样的事情。 它工作正常。