Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change a Windows Service's startup type in .NET (post-install)?

I have a program that installs a service, and I'd like to be able to give the user the option later on to change the startup type to "Automatic".

The OS is XP - if it makes any difference (Windows APIs?).

How can I do this in .NET? C# if possible! :)

like image 345
joshcomley Avatar asked Sep 25 '09 14:09

joshcomley


People also ask

What is the startup type for the Windows Update service?

I check all my machines, include Server 2012R2, Server 2016 and Server 2019, even 2 Windows 10 machines, the Startup type of Windows update service on all of devices are Manual, not Automatic or Automatic Delayed Start.

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.


1 Answers

I wrote a blog post on how to do this using P/Invoke. Using the ServiceHelper class from my post you can do the following to change the Start Mode.

var svc = new ServiceController("ServiceNameGoesHere");   ServiceHelper.ChangeStartMode(svc, ServiceStartMode.Automatic);  
like image 136
Peter Kelly Avatar answered Sep 29 '22 20:09

Peter Kelly