Provides a Windows Service Class Library (basic functions)

Source: Internet
Author: User

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 );}}

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.