通过C#调用cmd来修改服务启动类型
程序员文章站
2023-11-24 14:42:28
可以使用system.serviceprocess.servicecontroller这个类允许连接到正在运行或者已停止的服务、对其进行操作或获取有关它的信息。servic...
可以使用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;
}
}
}
上一篇: blender怎么制作音乐频谱动画效果?
下一篇: .Net Winform开发笔记(一)
推荐阅读