This should do the trick:
var serviceName = "<your service name here>";
string objPath = string.Format("Win32_Service.Name='{0}'", serviceName);
using (var service = new ManagementObject(new ManagementPath(objPath)))
{
var result = (int)service.InvokeMethod("ChangeStartMode", new object[] {"Automatic"});
}
You'll need to add a reference to the System.Management
assembly, as well as import the namespace System.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 result
variable 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.