Automatically start a Windows Service on install?

In your Installer class, add a handler for the AfterInstall event. You can then call the ServiceController in the event handler to start the service.

In your Installer class, add a handler for the AfterInstall event. You can then call the ServiceController in the event handler to start the service. Public ServiceInstaller() { //... Installer code here this.

AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall); } void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e) { ServiceController sc = new ServiceController("ServiceName"); sc.Start(); } Now when you run InstallUtil on your installer it will install and then start up the service.

5 (comment from a proposed edit): Better to use serviceInstaller. ServiceName, if the servicename gets changed it will use the correct name without needing to change it in the code. – Marc Gravell?

Feb 1 at 9:46.

Programmatic options for controlling services: Native code can used, "Starting a Service". Maximum control with minimum dependencies but the most work. WMI: Win32_Service has a StartService method.

This is good for cases where you need to be able to perform other processing (e.g. To select which service). PowerShell: execute Start-Service via RunspaceInvoke or by creating your own Runspace and using its CreatePipeline method to execute. This is good for cases where you need to be able to perform other processing (e.g. To select which service) with a much easier coding model than WMI, but depends on PSH being installed.

A . NET application can use ServiceController.

Use ServiceController to start your service from code. Update: And more correct way to start service from the command line is to use "sc" (Service Controller) command instead of "net".

After refactoring a little bit, this is an example of a complete windows service installer with automatic start: using System. ComponentModel; using System.Configuration. Install; using System.

ServiceProcess; namespace Example.of.name. Space { RunInstaller(true) public partial class ServiceInstaller : Installer { private readonly ServiceProcessInstaller processInstaller; private readonly System.ServiceProcess. ServiceInstaller serviceInstaller; public ServiceInstaller() { InitializeComponent(); processInstaller = new ServiceProcessInstaller(); serviceInstaller = new System.ServiceProcess.

ServiceInstaller(); // Service will run under system account processInstaller. Account = ServiceAccount. LocalSystem; // Service will have Start Type of Manual serviceInstaller.

StartType = ServiceStartMode. Automatic; serviceInstaller. ServiceName = "Windows Automatic Start Service"; Installers.

Add(serviceInstaller); Installers. Add(processInstaller); serviceInstaller. AfterInstall += ServiceInstaller_AfterInstall; } private void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e) { ServiceController sc = new ServiceController("Windows Automatic Start Service"); sc.Start(); } } }.

You can use the following command line to start the service: net start *servicename.

Automatic startup means that the service is automatically started when Windows starts. As others have mentioned, to start it from the console you should use the ServiceController.

I do not wish to do this. I am looking to do this in one go from the command line, or from within the Windows Service classes. – mickyjtwin Jun 24 '09 at 6:41 Sorry, my bad, I missed the point where you explicitly excluded the possibility of starting it over the control panel.

– Michael Barth Jun 24 '09 at 6:54.

You can use the GetServices method of ServiceController class to get an array of all the services. Then, find your service by checking the ServiceName property of each service. When you've found your service, call the Start method to start it.

You should also check the Status property to see what state it is already in before calling start (it may be running, paused, stopped, etc..).

You corrupted your designer. ReAdd your Installer Component. It should have a serviceInstaller and a serviceProcessInstaller.

The serviceInstaller with property Startup Method set to Automatic will startup when installed and after each reboot.

You don't say what operating system you are using, but I remember running into this when developing an installer app last year. I beleive this is a security restriction in Windows 7, Server 2003/2008, and possibly Vista, as installation program cannot start any application program that it installs. If you set the service for Automatic Startup, then it should start the next time the system restarts.

I think what you're saying is that there are two issues are going on here. The first issue is that the service doesn't start upon install. The second is that the service is unable to start the process.

Am I right?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions