C # Operation Service

Source: Internet
Author: User
// 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 ;}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.