1. reference the system. serviceprocess namespace
Using system. serviceprocess;
2. Declare the servicecontroller variable
Private servicecontroller _ controller;
3. Assume that the service name is servicesname. Write the following code to start and stop the service:
Private void stopservice () {This. _ controller = new servicecontroller ("servicesname"); this. _ controller. stop (); this. _ controller. waitforstatus (servicecontrollerstatus. stopped); this. _ controller. close ();} private void startservice () {This. _ controller = new servicecontroller ("servicesname"); this. _ controller. start (); this. _ controller. waitforstatus (servicecontrollerstatus. running); this. _ controller. close ();} private void resetservice () {This. _ controller = new servicecontroller ("servicesname"); this. _ controller. stop (); this. _ controller. waitforstatus (servicecontrollerstatus. stopped); this. _ controller. start (); this. _ controller. waitforstatus (servicecontrollerstatus. running); this. _ controller. close ();}