C # control the startup and shutdown of Windows Services

Source: Internet
Author: User

 //...... <Br/> using system. serviceprocess; <br/> //...... <br/> /// <summary> <br/> // restart Windows Service <br/> /// </Summary> <br/> // <Param name = "servicename"> the Windows Service display name </param> <br/> // <returns> If the restart successfully return true else return false </returns> <br /> Public static bool restartwindowsservice (string servicename) <br/>{< br/> bool bresult = false; <br/> try <br/> {<br/> stopwindowsservice (servicename); <br/> thread. sleep (1000); <br/>}< br/> catch (exception ex) <br/>{< br/> startwindowsservice (servicename); <br/> thread. sleep (1000); <br/> stopwindowsservice (servicename); <br/> thread. sleep (1000); <br/> console. writeline (ex. message); <br/>}< br/> try <br/>{< br/> startwindowsservice (servicename); <br/> thread. sleep (1000); <br/>}< br/> catch (exception ex) <br/>{< br/> stopwindowsservice (servicename); <br/> thread. sleep (1000); <br/> startwindowsservice (servicename); <br/> thread. sleep (1000); <br/> console. writeline (ex. message); <br/>}< br/> bresult = true; <br/>}< br/> catch (exception ex) <br/>{< br/> bresult = false; <br/> throw ex; <br/>}< br/> return bresult; <br/>}</P> <p> // <summary> <br/> // start Windows Service <br/> /// </Summary> <br/> /// <Param name = "servicename"> the Windows Service display name </param> <br/> /// <returns> If the start successfully return true else return false </returns> <br/> Public static bool stopwindowsservice (string servicename) <br/> {<br/> servicecontroller [] SCS = servicecontroller. getservices (); <br/> bool bresult = false; <br/> foreach (servicecontroller SC in SCS) <br/>{< br/> If (SC. displayname = servicename) <br/>{< br/> try <br/>{< br/> SC. waitforstatus (servicecontrollerstatus. running, timespan. fromseconds (30); <br/> SC. stop (); <br/> bresult = true; <br/>}< br/> catch (exception ex) <br/>{< br/> bresult = false; <br/> throw ex; <br/>}< br/> return bresult; <br/>}</P> <p> // <summary> <br/> // stop Windows Service <br/> /// </Summary> <br/> /// <Param name = "servicename"> the Windows Service display name </param> <br/> /// <returns> If the stop successfully return true else return false </returns> <br/> Public static bool startwindowsservice (string servicename) <br/> {<br/> servicecontroller [] SCS = servicecontroller. getservices (); <br/> bool bresult = false; <br/> foreach (servicecontroller SC in SCS) <br/>{< br/> If (SC. displayname = servicename) <br/>{< br/> try <br/>{< br/> SC. waitforstatus (servicecontrollerstatus. stopped, timespan. fromseconds (30); <br/> SC. start (); <br/> bresult = true; <br/>}< br/> catch (exception ex) <br/>{< br/> bresult = false; <br/> throw ex; <br/>}< br/> return bresult; <br/>}

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.