//...... <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/>}