通過C#調(diào)用cmd來修改服務(wù)啟動類型
更新時間:2012年12月16日 14:42:04 作者:
可以使用System.ServiceProcess.ServiceController這個類允許連接到正在運行或者已停止的服務(wù)、對其進行操作或獲取有關(guān)它的信息但是這個類并沒有提供修改服務(wù)啟動類型的方法,可以通過C#調(diào)用cmd來修改
可以使用System.ServiceProcess.ServiceController這個類允許連接到正在運行或者已停止的服務(wù)、對其進行操作或獲取有關(guān)它的信息。ServiceController 提供了開始和停止服務(wù)的方法(Start, Stop)。
但是這個類并沒有提供修改服務(wù)啟動類型的方法,可以通過C#調(diào)用cmd來修改
參考網(wǎng)上的這個方法:
//設(shè)置服務(wù)的啟動類型
//sServiceName服務(wù)名稱
//iStartType要設(shè)置的啟動類型 2:自動,3:手動,4:禁用
//返回成功狀態(tài) 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; //服務(wù)名稱
private ServiceController sc;
/// <summary>
/// 服務(wù)操作
/// </summary>
/// <param name="ServiceName">服務(wù)名稱</param>
public ClassService(string ServiceName)
{
this.ServiceName = ServiceName;
this.sc = new ServiceController(ServiceName);
}
/// <summary>
/// 啟動服務(wù)
/// </summary>
/// <returns></returns>
public bool StartService()
{
try
{
if (sc.Status != ServiceControllerStatus.Running)
sc.Start();
else
return false;
}
catch (Exception)
{
return false;
}
return true;
}
/// <summary>
/// 停止服務(wù)
/// </summary>
/// <returns></returns>
public bool StopService()
{
try
{
if (sc.Status != ServiceControllerStatus.Stopped)
sc.Stop();
else
return false;
}
catch (Exception)
{
return false;
}
return true;
}
/// <summary>
/// 重啟服務(wù)
/// </summary>
/// <returns></returns>
public bool RestartService()
{
if (StopService())
{
if (StartService())
return true;
else
return false;
}
else
return false;
}
/// <summary>
/// 獲取服務(wù)啟動類型
/// </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>
/// 設(shè)置服務(wù)啟動類型
/// </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;
}
}
}
但是這個類并沒有提供修改服務(wù)啟動類型的方法,可以通過C#調(diào)用cmd來修改
參考網(wǎng)上的這個方法:
//設(shè)置服務(wù)的啟動類型
//sServiceName服務(wù)名稱
//iStartType要設(shè)置的啟動類型 2:自動,3:手動,4:禁用
//返回成功狀態(tài) true:成功,false:失敗
復(fù)制代碼 代碼如下:
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;
}
}
復(fù)制代碼 代碼如下:
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; //服務(wù)名稱
private ServiceController sc;
/// <summary>
/// 服務(wù)操作
/// </summary>
/// <param name="ServiceName">服務(wù)名稱</param>
public ClassService(string ServiceName)
{
this.ServiceName = ServiceName;
this.sc = new ServiceController(ServiceName);
}
/// <summary>
/// 啟動服務(wù)
/// </summary>
/// <returns></returns>
public bool StartService()
{
try
{
if (sc.Status != ServiceControllerStatus.Running)
sc.Start();
else
return false;
}
catch (Exception)
{
return false;
}
return true;
}
/// <summary>
/// 停止服務(wù)
/// </summary>
/// <returns></returns>
public bool StopService()
{
try
{
if (sc.Status != ServiceControllerStatus.Stopped)
sc.Stop();
else
return false;
}
catch (Exception)
{
return false;
}
return true;
}
/// <summary>
/// 重啟服務(wù)
/// </summary>
/// <returns></returns>
public bool RestartService()
{
if (StopService())
{
if (StartService())
return true;
else
return false;
}
else
return false;
}
/// <summary>
/// 獲取服務(wù)啟動類型
/// </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>
/// 設(shè)置服務(wù)啟動類型
/// </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# 執(zhí)行CMD命令并接收返回結(jié)果的操作方式
- C# 調(diào)用命令行執(zhí)行Cmd命令的操作
- C# 使用相同權(quán)限調(diào)用 cmd 傳入命令的方法
- C#隱式運行CMD命令(隱藏命令窗口)
- C#調(diào)用CMD命令實例
- c#通過進程調(diào)用cmd判斷登錄用戶權(quán)限代碼分享
- C# cmd中修改顯示(顯示進度變化效果)的方法
- C#中調(diào)用命令行cmd開啟wifi熱點的實例代碼
- 在asp.net(c#)下實現(xiàn)調(diào)用cmd的方法
- C#中隱式運行CMD命令行窗口的方法
- C#程序調(diào)用cmd.exe執(zhí)行命令
相關(guān)文章
winform實現(xiàn)創(chuàng)建最前端窗體的方法
這篇文章主要介紹了winform實現(xiàn)創(chuàng)建最前端窗體的方法,涉及C#窗體屬性設(shè)置的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-08-08c#入門之分支語句使用方法(三元運算符、if語句、switch語句)
這篇文章主要介紹了c#入門之分支語句使用方法,包括三元運算符、if語句、switch語句,需要的朋友可以參考下2014-04-04