public static bool SetServiceStartMode(string serviceName, StartMode startMode, out uint retValue)
{
if (string.IsNullOrEmpty(serviceName)) throw new ArgumentNullException("serviceName");
try
{
ManagementPath myPath = new ManagementPath();
myPath.Server = System.Environment.MachineName;
myPath.NamespacePath = @"root\CIMV2";
myPath.RelativePath = "Win32_Service.Name='" + serviceName + "'";
using (ManagementObject service = new ManagementObject(myPath))
{
string mode = ConvertStartModeToString(startMode);
object[] inputArgs = new object[] { mode };
retValue = (uint)service.InvokeMethod("ChangeStartMode", inputArgs);
return retValue == 0;
}
}
catch (Exception e)
{
retValue = 8;
Utils.Trace(e, "Unexpected error setting start mode for service {0}.", serviceName);
return false;
}
}