Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically start your service after install?

How do you automatically start a service after running an install from a Visual Studio Setup Project?

I just figured this one out and thought I would share the answer for the general good. Answer to follow. I am open to other and better ways of doing this.

like image 993
Jason Z Avatar asked Oct 17 '08 15:10

Jason Z


People also ask

How do I start a service automatically?

Go to start type services. msc and press enter. On the services list that opens up, right click on the service and select Properties. The dialog that opens has an option 'Automatic' for starting your service.

How do I start Windows Service after auto 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. Now when you run InstallUtil on your installer, it will install and then start up the service automatically.

How do I start a service in C#?

Go to Visual C# and select Classic Desktop and from the next window, choose Windows Service. Name the service “MyFirstWindowsService” and click to OK. It will add a Windows Service for you. Rename the Service1.


1 Answers

Add the following class to your project.

using System.ServiceProcess;    class ServInstaller : ServiceInstaller {     protected override void OnCommitted(System.Collections.IDictionary savedState)     {         ServiceController sc = new ServiceController("YourServiceNameGoesHere");         sc.Start();     } } 

The Setup Project will pick up the class and run your service after the installer finishes.

like image 141
Jason Z Avatar answered Oct 11 '22 17:10

Jason Z