This article transferred from: http://www.cnblogs.com/hb_cattle/archive/2011/12/04/2275319.html
Given the ease of deployment, we typically create a Windows service written in C # as an installation package. After the service installation is complete, it is very inconvenient to start the service manually for the first time. Looking at some of the information on the Internet, we found that one way to start the service is to invoke the command line in the installation completion event, which is feasible but not perfect. Well, here's a look at how to do a better job of starting the service automatically.
1, rewrite the ProjectInstaller of the Commit method
Using System;Using System.Collections;Using System.Collections.Generic;Using System.ComponentModel;Using System.Configuration.Install;using system.linq; using system.serviceprocess; namespace cleanexpiredsessionseivice{[Runinstaller (true)] public partial class ProjectInstaller: System.Configuration.Install.Installer {public ProjectInstaller () {InitializeComponent (); Span class= "KWRD" >public override void Commit (IDictionary savedstate) {base.commit (savedstate); //auot start service after the installation is completed ServiceController sc = new S Ervicecontroller ( "Cleanexpiredsessionseivice"); if (SC. Status.equals (servicecontrollerstatus.stopped)) {SC. Start (); } } }}
2. Add the custome Action named commit in the service Setup project
Right-click on the Service Setup project and select View-custom Actions from the popup menu
Then right-click on the Commit and select Add Custom Action ... and select Application Folder in the list box that pops up. The final result is as follows:
It is important to note that the second step is necessary, otherwise the service will not start automatically. My personal understanding is that the commit custom action automatically calls ProjectInstaller's Commit method, where the commit custom action plays the role of a caller.
[Go to].net when the Windows Service installation is complete automatically starts