Windows Service Status Auto-start

Source: Internet
Author: User

Many people create a Windows Service installation package when found clearly in the properties set automatic start, can be completed after the service installation, but also need to manually start the service, I have a full implementation of the automatic Start method

In the ProjectInstaller.cs file on the line, the direct code is as follows:

1  Public Partial classProjectInstaller:System.Configuration.Install.Installer2     {3          PublicProjectInstaller ()4         {5 InitializeComponent ();6             //set up auto-start after installation7              This. Afterinstall + =NewInstalleventhandler (projectinstaller_afterinstall);8         }9         /// <summary>Ten         ///set up auto-start after installation One         ///the StartType in the ServiceInstaller1 to be set to Automatic, which indicates a random start, A         ///ServiceName represents the service name, -         ///Description represents a description of the service, -         ///DisplayName represents the display name.  the         ///the account in ServiceProcessInstaller1 is set to LocalSystem, which indicates the local system accounts -         /// </summary> -         /// <param name= "Sender" ></param> -         /// <param name= "E" ></param> +         Private voidProjectinstaller_afterinstall (Objectsender, Installeventargs e) -         { +Process p =NewProcess (); Ap.StartInfo.FileName ="Cmd.exe"; atP.startinfo.useshellexecute =false; -P.startinfo.redirectstandardinput =true; -P.startinfo.redirectstandardoutput =true; -P.startinfo.redirectstandarderror =true; -P.startinfo.createnowindow =true; - P.start (); in             stringCmdstring ="SC start Jieservice";//cmd command jieservice service name - P.standardinput.writeline (cmdstring); toP.standardinput.writeline ("Exit"); +         } -}
View Code

Note : In the code, "Jieservice" is the service name. Must remember to change, it is easy to ignore

Windows Service Status Auto-start

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.