Alex Rivera | Logout

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

Asked 2009-09-25T14:28:15.507
37

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! :)

Edit
Report

2 Answers

64

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); 
answered 2011-04-21T10:08:11.113
10

You can use the OpenService() and ChangeServiceConfig() native Win32 APIs for that purpose. I believe that there is some information on pinvoke.net and of course on MSDN. You might want to check out the P/Invoke Interopt Assistant.

answered 2009-09-28T06:10:13.557

Your Answer