// 1. Installation Service: Private void installservice (idictionary statesaver, string filepath) {try {system. serviceprocess. servicecontroller service = new system. serviceprocess. servicecontroller ("servicename"); If (! Serviceisexisted ("servicename") {// install service assemblyinstaller myassemblyinstaller = new assemblyinstaller (); myassemblyinstaller. usenewcontext = true; myassemblyinstaller. path = filepath; myassemblyinstaller. install (statesaver); myassemblyinstaller. commit (statesaver); myassemblyinstaller. dispose (); // -- start service. start ();} else {If (service. status! = System. serviceprocess. servicecontrollerstatus. Running & service. status! = System. serviceprocess. servicecontrollerstatus. startpending) {service. start () ;}} catch (exception ex) {Throw new exception ("installserviceerror \ n" + ex. message) ;}}// 2. Uninstall the Windows Service: Private void uninstallservice (string filepath) {try {If (serviceisexisted ("servicename ")) {// uninstall service assemblyinstaller myassemblyinstaller = new assemblyinstaller (); myassemblyinstaller. usenewcontext = True; myassemblyinstaller. path = filepath; myassemblyinstaller. uninstall (null); myassemblyinstaller. dispose () ;}} catch (exception ex) {Throw new exception ("uninstallserviceerror \ n" + ex. message) ;}}// 3. Determine whether the window service exists: Private bool serviceisexisted (string servicename) {servicecontroller [] services = servicecontroller. getservices (); foreach (servicecontroller s in services) {If (S. servicena Me = servicename) {return true;} return false;} // 4. Start the service: Private void startservice (string servicename) {If (serviceisexisted (servicename) {system. serviceprocess. servicecontroller service = new system. serviceprocess. servicecontroller (servicename); If (service. status! = System. serviceprocess. servicecontrollerstatus. Running & service. status! = System. serviceprocess. servicecontrollerstatus. startpending) {service. start (); For (INT I = 0; I <60; I ++) {service. refresh (); system. threading. thread. sleep (1000); If (service. status = system. serviceprocess. servicecontrollerstatus. running) {break;} if (I = 59) {Throw new exception (startserviceerror. replace ("$ S $", servicename) ;}}}// 5. Stop the service: Private void stopservice (string servicename) {If (serviceisexisted (servicename) {system. serviceprocess. servicecontroller service = new system. serviceprocess. servicecontroller (servicename); If (service. status = system. serviceprocess. servicecontrollerstatus. running) {service. stop (); For (INT I = 0; I <60; I ++) {service. refresh (); system. threading. thread. sleep (1000); If (service. status = system. serviceprocess. servicecontrollerstatus. s Topped) {break;} if (I = 59) {Throw new exception (stopserviceerror. replace ("$ S $", servicename) ;}}}// stop the specified public string startservice (string servicename) {string strrst = NULL; managementobject mo = This. managementclass. createinstance (); Mo. path = new managementpath (this. strpath + ". name = \ "" + servicename + "\" "); try {If (string) Mo [" state "] =" STOPPED ")//! (Bool) Mo ["acceptstop"] Mo. invokemethod ("startservice", null);} catch (managementexception e) {strrst = E. message;} return strrst;} // pause the specified public string pauseservice (string servicename) {string strrst = NULL; managementobject mo = This. managementclass. createinstance (); Mo. path = new managementpath (this. strpath + ". name = \ "" + servicename + "\" "); try {// determine whether to suspend if (bool) Mo [" acceptpause "] & (string) mo ["state"] = "running") Mo. invokemethod ("pauseservice", null);} catch (managementexception e) {strrst = E. message;} return strrst;} // restore the specified public string resumeservice (string servicename) {string strrst = NULL; managementobject mo = This. managementclass. createinstance (); Mo. path = new managementpath (this. strpath + ". name = \ "" + servicename + "\" "); try {// determine whether the IF (bool) Mo [" acceptpause "] & (string) can be restored) mo ["state"] = "paused") Mo. invokemethod ("resumeservice", null);} catch (managementexception e) {strrst = E. message;} return strrst;} // stop the specified public string stopservice (string servicename) {string strrst = NULL; managementobject mo = This. managementclass. createinstance (); Mo. path = new managementpath (this. strpath + ". name = \ "" + servicename + "\" "); try {// determine whether to stop if (bool) Mo [" acceptstop "]) // (string) mo ["state"] = "running" mo. invokemethod ("stopservice", null);} catch (managementexception e) {strrst = E. message;} return strrst ;}}}