public static SetServiceStartMode ( string serviceName, StartMode startMode, uint &retValue ) : bool | ||
serviceName | string | The service name. |
startMode | StartMode | The new start mode. |
retValue | uint | The return value. /// Return value Description: /// 0 Success /// 1 Not Supported /// 2 Access Denied /// 3 Dependent Services Running /// 4 Invalid Service Control /// 5 Service Cannot Accept Control /// 6 Service Not Active /// 7 ervice Request Timeout /// 8 Unknown Failure /// 9 Path Not Found /// 10 Service Already Running /// 11 Service Database Locked /// 12 Service Dependency Deleted /// 13 Service Dependency Failure /// 14 Service Disabled /// 15 Service Logon Failure /// 16 Service Marked For Deletion /// 17 Service No Thread /// 18 Status Circular Dependency /// 19 Status Duplicate Name /// 20 Status Invalid Name /// 21 Status Invalid Parameter /// 22 Status Invalid Service Account /// 23 Status Service Exists /// 24 Service Already Paused /// |
return | bool |
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;
}
}