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