C # for Windows service Operations (registration installation service, uninstall service, start stop service, Judge service exists)

Source: Internet
Author: User

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>///&LT;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. "); } } } } } } }

Related Article

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.