通过C#调用cmd来修改服务启动类型
作者:
可以使用System.ServiceProcess.ServiceController这个类允许连接到正在运行或者已停止的服务、对其进行操作或获取有关它的信息但是这个类并没有提供修改服务启动类型的方法,可以通过C#调用cmd来修改
可以使用System.ServiceProcess.ServiceController这个类允许连接到正在运行或者已停止的服务、对其进行操作或获取有关它的信息。ServiceController 提供了开始和停止服务的方法(Start, Stop)。
但是这个类并没有提供修改服务启动类型的方法,可以通过C#调用cmd来修改
参考网上的这个方法:
//设置服务的启动类型
//sServiceName服务名称
//iStartType要设置的启动类型 2:自动,3:手动,4:禁用
//返回成功状态 true:成功,false:失败
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; //服务名称
private ServiceController sc;
/// <summary>
/// 服务操作
/// </summary>
/// <param name="ServiceName">服务名称</param>
public ClassService(string ServiceName)
{
this.ServiceName = ServiceName;
this.sc = new ServiceController(ServiceName);
}
/// <summary>
/// 启动服务
/// </summary>
/// <returns></returns>
public bool StartService()
{
try
{
if (sc.Status != ServiceControllerStatus.Running)
sc.Start();
else
return false;
}
catch (Exception)
{
return false;
}
return true;
}
/// <summary>
/// 停止服务
/// </summary>
/// <returns></returns>
public bool StopService()
{
try
{
if (sc.Status != ServiceControllerStatus.Stopped)
sc.Stop();
else
return false;
}
catch (Exception)
{
return false;
}
return true;
}
/// <summary>
/// 重启服务
/// </summary>
/// <returns></returns>
public bool RestartService()
{
if (StopService())
{
if (StartService())
return true;
else
return false;
}
else
return false;
}
/// <summary>
/// 获取服务启动类型
/// </summary>
/// <returns>2:自动 3:手动 4:禁用 0:获取失败</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>
/// 设置服务启动类型
/// </summary>
/// <param name="Mode">2:自动 3:手动 4:禁用</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;
}
}
}
但是这个类并没有提供修改服务启动类型的方法,可以通过C#调用cmd来修改
参考网上的这个方法:
//设置服务的启动类型
//sServiceName服务名称
//iStartType要设置的启动类型 2:自动,3:手动,4:禁用
//返回成功状态 true:成功,false:失败
复制代码 代码如下:
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; //服务名称
private ServiceController sc;
/// <summary>
/// 服务操作
/// </summary>
/// <param name="ServiceName">服务名称</param>
public ClassService(string ServiceName)
{
this.ServiceName = ServiceName;
this.sc = new ServiceController(ServiceName);
}
/// <summary>
/// 启动服务
/// </summary>
/// <returns></returns>
public bool StartService()
{
try
{
if (sc.Status != ServiceControllerStatus.Running)
sc.Start();
else
return false;
}
catch (Exception)
{
return false;
}
return true;
}
/// <summary>
/// 停止服务
/// </summary>
/// <returns></returns>
public bool StopService()
{
try
{
if (sc.Status != ServiceControllerStatus.Stopped)
sc.Stop();
else
return false;
}
catch (Exception)
{
return false;
}
return true;
}
/// <summary>
/// 重启服务
/// </summary>
/// <returns></returns>
public bool RestartService()
{
if (StopService())
{
if (StartService())
return true;
else
return false;
}
else
return false;
}
/// <summary>
/// 获取服务启动类型
/// </summary>
/// <returns>2:自动 3:手动 4:禁用 0:获取失败</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>
/// 设置服务启动类型
/// </summary>
/// <param name="Mode">2:自动 3:手动 4:禁用</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;
}
}
}