如何以编程方式停止/启动远程盒子上的 Windows 服务?
问题描述:
我想编写一个控制台或 Click Once WinForms 应用程序,它将以编程方式停止和/或启动远程框上的 Windows 服务.
I want to write a console or Click Once WinForms app that will programmatically stop and/or start a windows service on a remote box.
两个盒子都运行 .NET 3.5 - 有哪些 .NET API 可以实现这一点?
Both boxes are running .NET 3.5 - what .NET API's are available to accomplish this?
答
在 C# 中:
var sc = new System.ServiceProcess.ServiceController("MyService", "MyRemoteMachine");
sc.Start();
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
sc.Stop();
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);