使用SC在C#中取得windows服务的启动类型
2008-03-19 21:33
423 查看
//取得服务的启动类型的当前状态,返回启动类型的状态値
//2:自动,3:手动,4:禁用 ......
//sServiceName服务名称
//sState输出相关的状态代码,失败则为错误消息
public int GetWindowsServiceStartType(String sServiceName, out String sState)
{
sState = "";
try
{
System.Diagnostics.ProcessStartInfo objProcessInf = new System.Diagnostics.ProcessStartInfo();
objProcessInf.FileName = "cmd.exe";
objProcessInf.UseShellExecute = false;
objProcessInf.RedirectStandardError = true;
objProcessInf.RedirectStandardInput = true;
objProcessInf.RedirectStandardOutput = true;
objProcessInf.CreateNoWindow = true;
objProcessInf.Arguments = "/c sc qc " + sServiceName;
System.Diagnostics.Process objProcess = System.Diagnostics.Process.Start(objProcessInf);
String sStateValue = objProcess.StandardOutput.ReadToEnd();
if (sStateValue.IndexOf("AUTO_START") > 0)
{
sState = "AUTO_START";
return 2;
}
if (sStateValue.IndexOf("DEMAND_START") > 0)
{
sState = "DEMAND_START";
return 3;
}
if (sStateValue.IndexOf("DISABLED") > 0)
{
sState = "DISABLED";
return 4;
}
return 0;
}
catch(Exception e)
{
sState = e.Message;
return -1;
}
}
//设置服务的启动类型
//sServiceName服务名称
//iStartType要设置的启动类型
//返回成功状态 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;
}
}
//2:自动,3:手动,4:禁用 ......
//sServiceName服务名称
//sState输出相关的状态代码,失败则为错误消息
public int GetWindowsServiceStartType(String sServiceName, out String sState)
{
sState = "";
try
{
System.Diagnostics.ProcessStartInfo objProcessInf = new System.Diagnostics.ProcessStartInfo();
objProcessInf.FileName = "cmd.exe";
objProcessInf.UseShellExecute = false;
objProcessInf.RedirectStandardError = true;
objProcessInf.RedirectStandardInput = true;
objProcessInf.RedirectStandardOutput = true;
objProcessInf.CreateNoWindow = true;
objProcessInf.Arguments = "/c sc qc " + sServiceName;
System.Diagnostics.Process objProcess = System.Diagnostics.Process.Start(objProcessInf);
String sStateValue = objProcess.StandardOutput.ReadToEnd();
if (sStateValue.IndexOf("AUTO_START") > 0)
{
sState = "AUTO_START";
return 2;
}
if (sStateValue.IndexOf("DEMAND_START") > 0)
{
sState = "DEMAND_START";
return 3;
}
if (sStateValue.IndexOf("DISABLED") > 0)
{
sState = "DISABLED";
return 4;
}
return 0;
}
catch(Exception e)
{
sState = e.Message;
return -1;
}
}
//设置服务的启动类型
//sServiceName服务名称
//iStartType要设置的启动类型
//返回成功状态 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;
}
}
相关文章推荐
- 使用C#启动服务类型为Disabled的windows服务
- windows下使用sc创建服务 配置自动启动
- C#对Windows服务组的启动与停止操作
- c# windows服务状态、启动和停止服务
- C# windows服务:C#windows服务中的Timer控件的使用
- C#windows service C#windows 服务安装卸载SC
- 命令行 SC命令 及通过sc config 更该windows服务的启动类型等
- 使用批处理启动Windows平台上的Oracle服务
- 使用advapi32.dll来启动、停止服务(c#)
- C#中使用Windows消息队列服务(MSMQ)简单示例
- 在windows下使用批处理启动停止服务
- c# 安装、注册windows service服务,判断服务是否存在,是否启动
- C# 使用windows程序控制window服务
- C# 以管理员方式启动Winform,进而使用管理员控制Windows Service
- 使用C#代码安装 Windows 服务--直接运行安装服务
- Windows服务用C++代码实现的一些操作-1:修改服务启动类型
- 用C#开发Windows服务监控系统使用
- 安装 启动 停止 卸载 Windows服务 c#