The previous section describes how to create and install a service (create a windows Service). The following describes how to start, stop, and determine whether to start the service. [Csharp] /// <summary> /// determine whether a service is installed /// </summary> /// <param name = "serviceName"> </param> /// <returns> </returns> public static bool ISWindowsServiceInstalled (string serviceName) {try {ServiceController [] services = ServiceController. getServices (); foreach (ServiceController service in services) {if (service. serviceName = serviceName) {return true;} return false;} catch {return false ;}} [csharp // <summary> /// start a service /// </summary> /// <param name = "serviceName"> </param> public static void StartService (string serviceName) {try {ServiceController [] services = ServiceController. getServices (); foreach (ServiceController service in services) {if (service. serviceName = serviceName) {service. start (); service. waitForStatus (ServiceControllerStatus. running, new TimeSpan (0, 0, 30 ));}}} catch {} [csharp] // <summary> // stop a service // </summary> // <param name = "serviceName"> </param> public static void StopService (string serviceName) {try {ServiceController [] services = ServiceController. getServices (); foreach (ServiceController service in services) {if (service. serviceName = serviceName) {service. stop (); service. waitForStatus (ServiceControllerStatus. running, new TimeSpan (0, 0, 30 ));}}} catch {}} [csharp] /// <summary> /// determine whether a service is started // </summary> /// <param name = "serviceName"> </param> public static bool ISStart (string serviceName) {bool result = true; try {ServiceController [] services = ServiceController. getServices (); foreach (ServiceController service in services) {if (service. serviceName = serviceName) {if (service. status = ServiceControllerStatus. stopped) | (service. status = ServiceControllerStatus. stopPending) {result = false ;}}} catch {} return result ;}