I’m looking for a C# version of the following command:
sc config "someServiceName" start=auto
I’ve found a lot of information about configuring a service to start automatically as you install it, but I’m having trouble finding out how to do the same for an existing service.
Right now, I’ve resorted to shelling it out, but if there’s a way to use .NET APIs, I’d much rather do that.
This should do the trick:
You’ll need to add a reference to the
System.Managementassembly, as well as import the namespaceSystem.Management.Note that your program must elevated (running as an Administrator) for this to work, and there is no way around that. For other possible values for
ChangeStartMode, you can refer to MSDN.The
resultvariable will be a numeric value that indicates the result. For example, 0 for Success. Refer the the previously linked MSDN article for all of the possible return values.