Http://www.chenjiliang.com/Article/View.aspx? ArticleID = 14417
This class library is only applicable to basic Windows service operations and does not involve in-depth operations. I think it is enough.
You can modify batches and dependencies.
Copy and save
/// <Summary>/// Windows Service Class/// </Summary>UsingSystem;Public ClassServiceutil {/// <Summary> /// Verify whether the service exists /// </Summary> /// <Param name = "servicename"> Windows Service name </param> /// <Returns> If yes, true is returned; otherwise, false is returned. </returns> Public Static BoolServiceisexisted (StringServicename) {servicecontroller [] services = servicecontroller. getservices ();Foreach(Servicecontroller sInServices)If(S. servicename. tolower () = servicename. tolower ())Return True;Return False;}/// <Summary> /// Install the service /// </Summary> /// <Param name = "statesaver"> set </param> /// <Param name = "filepath"> Windows service file </param> Public Static VoidInstallservice (idictionary statesaver,StringFilepath ){Try{Assemblyinstaller myassemblyinstaller =NewAssemblyinstaller (); myassemblyinstaller. usenewcontext =True; Myassemblyinstaller. Path = filepath; myassemblyinstaller. Install (statesaver); myassemblyinstaller. Commit (statesaver); myassemblyinstaller. Dispose ();}Catch(Exception ex ){Throw NewSysexception (ex. Message, ex );}}/// <Summary> /// Uninstall the service /// </Summary> /// <Param name = "filepath"> Windows service file </param> Public Static VoidUninstallservice (StringFilepath ){Try{Assemblyinstaller myassemblyinstaller =NewAssemblyinstaller (); myassemblyinstaller. usenewcontext =True; Myassemblyinstaller. Path = filepath; myassemblyinstaller. Uninstall (Null); Myassemblyinstaller. Dispose ();}Catch(Exception ex ){Throw NewSysexception (ex. Message, ex );}}/// <Summary> /// Determine whether the Windows service is running /// </Summary> /// <Param name = "name"> Windows Service name </param> /// <Returns> /// <C> true </C> If the specified name is run; otherwise, <C> false </C>. /// </Returns> Public Static BoolIsrun (StringName ){BoolIsrun =False;Try{If(! Serviceisexisted (name ))Return False;VaRSC =NewServicecontroller (name );If(SC. Status = servicecontrollerstatus. startpending | SC. Status = servicecontrollerstatus. Running) {isrun =True;} SC. Close ();}Catch{Isrun =False;}ReturnIsrun ;}/// <Summary> /// Start the service /// </Summary> /// <Param name = "name"> service name </param> /// <Returns> true is returned if startup is successful; otherwise, false is returned. </returns> Public Static BoolStarservice (StringName ){Try{VaRSC =NewServicecontroller (name );If(SC. Status = servicecontrollerstatus. Stopped | SC. Status = servicecontrollerstatus. stoppending) {SC. Start (); SC. waitforstatus (servicecontrollerstatus. running,NewTimespan (0, 0, 10 ));}Else{} SC. Close ();Return True;}Catch(Exception ex ){Throw NewSysexception (ex. Message, ex );}}/// <Summary> /// Stop the service /// </Summary> /// <Param name = "name"> service name </param> /// <Returns> true is returned if the stop is successful; otherwise, false is returned. </returns> Public Static BoolStopservice (StringName ){Try{VaRSC =NewServicecontroller (name );If(SC. Status = servicecontrollerstatus. Running | SC. Status = servicecontrollerstatus. startpending) {SC. Stop (); SC. waitforstatus (servicecontrollerstatus. stopped,NewTimespan (0, 0, 10 ));}Else{} SC. Close ();Return True;}Catch(Exception ex ){Throw NewSysexception (ex. Message, ex );}}/// <Summary> /// Restart the service /// </Summary> /// <Param name = "name"> service name </param> /// <Returns> If the restart succeeds, true is returned; otherwise, false is returned. </returns> Public Static BoolRefreshservice (StringName ){ReturnStopservice (name) & starservice (name );}}