Modify the service startup type by calling C to cmd

  • 2020-05-09 19:08:05
  • OfStack

You can use the class System.ServiceProcess.ServiceController to connect to, manipulate, or obtain information about a running or defunct service. ServiceController provides methods to start and stop services (Start, Stop).
However, this class does not provide methods to modify the service startup type, which can be modified by C# calling cmd

Consider this method on the web:
// sets the startup type of the service
/ / sServiceName service name
//iStartType startup type to be set 2: automatic, 3: manual, 4: disabled
// returns the success status true: success, false: failure
 
public Boolean SetWindowsServiceStartType(String sServiceName, int iStartType) 
{ 
try 
{ 
System.Diagnostics.ProcessStartInfo objProcessInf = new System.Diagnostics.ProcessStartInfo(); 
objProcessInf.FileName = "cmd.exe"; 
objProcessInf.CreateNoWindow = false; 
objProcessInf.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
string sStartState = "boot"; 
switch (iStartType) 
{ 
case 1: 
{ 
sStartState = "system"; 
break; 
} 
case 2: 
{ 
sStartState = "auto"; 
break; 
} 
case 3: 
{ 
sStartState = "demand"; 
break; 
} 
case 4: 
{ 
sStartState = "disabled"; 
break; 
} 
default: 
{ 
break; 
} 
} 
objProcessInf.Arguments = "/c sc config " + sServiceName + " start= " + sStartState; 
System.Diagnostics.Process.Start(objProcessInf); 
return true; 
} 
catch 
{ 
return false; 
} 
} 

 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Diagnostics; 
using System.ServiceProcess; 
using Microsoft.Win32; 
namespace Service 
{ 
public class ClassService 
{ 
private string ServiceName; // The service name  
private ServiceController sc; 
/// <summary> 
///  Service operation  
/// </summary> 
/// <param name="ServiceName"> The service name </param> 
public ClassService(string ServiceName) 
{ 
this.ServiceName = ServiceName; 
this.sc = new ServiceController(ServiceName); 
} 
/// <summary> 
///  Start the service  
/// </summary> 
/// <returns></returns> 
public bool StartService() 
{ 
try 
{ 
if (sc.Status != ServiceControllerStatus.Running) 
sc.Start(); 
else 
return false; 
} 
catch (Exception) 
{ 
return false; 
} 
return true; 
} 
/// <summary> 
///  Stop the service  
/// </summary> 
/// <returns></returns> 
public bool StopService() 
{ 
try 
{ 
if (sc.Status != ServiceControllerStatus.Stopped) 
sc.Stop(); 
else 
return false; 
} 
catch (Exception) 
{ 
return false; 
} 
return true; 
} 
/// <summary> 
///  Restart the service  
/// </summary> 
/// <returns></returns> 
public bool RestartService() 
{ 
if (StopService()) 
{ 
if (StartService()) 
return true; 
else 
return false; 
} 
else 
return false; 
} 
/// <summary> 
///  Gets the service startup type  
/// </summary> 
/// <returns>2: automatic  3: manual  4: disable  0: For failure </returns> 
public int GetServiceStartMode() 
{ 
int Mode = 0; 
try 
{ 
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\services\" + ServiceName); 
Mode = Convert.ToInt32(key.GetValue("Start")); 
key.Close(); 
} 
catch (Exception) 
{ 
return 0; 
} 
return Mode; 
} 
/// <summary> 
///  Set the service startup type  
/// </summary> 
/// <param name="Mode">2: automatic  3: manual  4: disable </param> 
/// <returns></returns> 
public bool SetServiceStartMode(int Mode) 
{ 
try 
{ 
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\services\" + ServiceName, true); 
key.SetValue("Start", Mode); 
key.Close(); 
} 
catch (Exception) 
{ 
return false; 
} 
return true; 
} 
} 
} 

Related articles: