如何以编程方式重新启动 windows 服务?

How to restart windows service programmatically?

使用 ServiceController 从 Windows 表单应用程序启动我的 windows 服务。但这是行不通的。

ServiceController service = new ServiceController(serviceName);

此声明无效。

是否有任何其他方法可以从我的应用程序启动 windows 服务。

Service Display Name: Duress Alert Service
Service Name: Service1

如何在 C# 中重新启动我的服务?

非常感谢您的帮助!谢谢!

编辑 1:

根据 MSDN 来自以下答案:

ServiceController sc  = new ServiceController();
sc.ServiceName = "Service1";

ServiceName下显示红线,错误:

ServiceController does not contains the definition of ServiceName. No extension method 'ServiceName' no accepting a first argument of type controller could not be found (are you missing a using directive or an assembly reference?)

虽然我添加了服务流程的引用:

using System.ServiceProcess;

但仍然显示错误。

您将需要使用 StartStop 方法。有关示例,请参阅 https://msdn.microsoft.com/en-us/library/yb9w7ytd%28v=vs.110%29.aspx