Restart a windows services from C#

被刻印的时光 ゝ 提交于 2019-12-11 07:36:39

问题


How do I restart a currently running service in C#.


回答1:


You can use ServiceController. Example here.




回答2:


Use the ServiceController class.




回答3:


ServiceController _ServiceController = new ServiceController([NameService]);
if (_ServiceController.ServiceHandle != null) 
{
     _ServiceController.Stop();
     _ServiceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromMilliseconds([Time]));

     _ServiceController.Start();
     _ServiceController.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromMilliseconds([Time]));
}



回答4:


You'll also want to make sure the user has the proper authentication access (UAC control) on the system. If they don't have the proper access, you'll wind up with an exception in code.



来源:https://stackoverflow.com/questions/3312656/restart-a-windows-services-from-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!