Search code examples
.netwindows-services

How can I programmatically stop/start a windows service on a remote box?


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.

Both boxes are running .NET 3.5 - what .NET API's are available to accomplish this?


Solution

  • in 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);