<summary>
Determine if 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;}
}
///<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 {}}
///<summary>///Stop a service///</summary>//<param Nam E= "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 {}}
<summary>/// Determine if 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; }
About operations for Windows services