Thursday, May 14, 2009

Use ServiceController to Start or Stop a Service

Reference the System.ServiceProcess.dll first, and use the namespace System.ServiceProcess:

We write the code sample as the following:

using System;
using System.ServiceProcess;

class Program
{
static void Main(string[] args)
{
ServiceController sc = new ServiceController("Server");
string svStatus = sc.Status.ToString();
if (svStatus == "Stopped")
{ sc.Start(); }
if (svStatus == "Running")
{ sc.Stop(); }
}
}

No comments: