问题
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