To do the project needs to operate the Windows service, from the Internet to find some information, summed up as follows:
(The following program tests through the program)
Using System; Using System.Collections.Generic; Using System.ComponentModel; Using System.Data; Using System.Drawing; Using System.Text; Using System.Windows.Forms; Using System.Collections; Using System.Configuration.Install; Using System.Collections.Specialized; Using System.ServiceProcess; Namespace IAU. SERVERINSTALL.BLL {public class ServiceControl {///<summary>///Registration Service (started at the end of registration, the existing service started directly.) )///</summary>///<param name= "strservicename" > Service name </param>///<param name= " Strserviceinstallpath > Service Installer full path (. exe program full path) </param> public void Register (string strservicename, String Strserviceinstallpath) {IDictionary mysavedstate = new Hashtable (); try {System.ServiceProcess.ServiceController Service = new System.ServiceProcess.ServiceController (strservicename); If the service already exists, uninstall if (serviceisexisted (Strservicename)) {//stopservice (strservicename); Uninstallservice (Strservicename, Strserviceinstallpath); } service. Refresh (); Registration Service Assemblyinstaller MyAssemBlyinstaller = new Assemblyinstaller (); Mysavedstate.clear (); Myassemblyinstaller.path = Strserviceinstallpath; Myassemblyinstaller.usenewcontext = true; Myassemblyinstaller.install (mysavedstate); Myassemblyinstaller.commit (mysavedstate); Myassemblyinstaller.dispose (); Service. Start (); ' Catch (Exception ex) {throw new Exception ("Error registering Service:" + ex.) message); }///<summary>///Uninstall service///</summary>///<param name= "strservicename" > Service name </param>///<PA Ram Name= "Strserviceinstallpath" > Service Installer full path (. exe program full path) </param> public void Uninstallservice (string Strservicename, String strserviceinstallpath) {try {if (serviceisexisted (Strservicename)) {//uninstall Service ASSEMBL Yinstaller Myassemblyinstaller = new Assemblyinstaller (); Myassemblyinstaller.usenewcontext = true; Myassemblyinstaller.path = Strserviceinstallpath; Myassemblyinstaller.uninstall (NULL); Myassemblyinstaller.dispose (); } catch (Exception ex) {throw new Exception ("Error Uninstalling Service:" + ex.) message); }///<summary>///to determine if the service exists///</summary>///<param name= "serviceName" > Service name </param>///<r Eturns></returns> public bool Serviceisexisted (string serviceName) {servicecontroller[] services = Servicecontroller.getservices (); foreach (ServiceController s in services) {if (S.servicename = = ServiceName) {return true;}} return false; ///<summary>///Start Service (Start existing service, start failure error after 30 seconds)///</summary>///<param name= "serviceName" > Service name </ param> public 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 < i++) {service. Refresh (); System.Threading.Thread.Sleep (1000); if (service). Status = = System.serviceProcess.ServiceControllerStatus.Running) {break;} if (i = =) {throw new Exception (service + ServiceName +) failed to start. "); } } } } } <summary>///Stop Service (stop existing service, 30 seconds to stop failure error)///</summary>///<param name= "ServiceName" ></param > public 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 < i++) {service. Refresh (); System.Threading.Thread.Sleep (1000); if (service). Status = = System.ServiceProcess.ServiceControllerStatus.Stopped) {break;} if (i = =) {throw new Exception ("service" + Serv Icename + "Stop failure. "); } } } } } } }