使用C#控制Windows Service
程式碼片段
[引用ServiceProcess]
using System.ServiceProcess;
[呼叫指定的服務名稱。wuauserv為Win更新,其它名稱可手動到服務裡面去尋找]
ServiceController sc = new ServiceController("wuauserv");
[如果服務為啟動狀態則停止它]
if (sc != null && sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
}
sc.WaitForStatus(ServiceControllerStatus.Stopped);
sc.Close();
-----------------------------------------------------------------------------------------------------------
[引用Process,設置FileName為sc,引用參數,啟動process來對服務做操作]
using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
var startInfo = process.StartInfo;
startInfo.FileName = "sc";
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.Arguments = string.Format("failure \"{0}\" reset= 0 actions= \"\"/60000", "wuauserv");
//startInfo.Arguments = string.Format("config \"{0}\" start= disabled", "wuauserv");
process.Start();
process.WaitForExit();
}
截自參考
Service Controller
SC指令
SC的問答1
SC的問答2
[引用ServiceProcess]
using System.ServiceProcess;
[呼叫指定的服務名稱。wuauserv為Win更新,其它名稱可手動到服務裡面去尋找]
ServiceController sc = new ServiceController("wuauserv");
[如果服務為啟動狀態則停止它]
if (sc != null && sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
}
sc.WaitForStatus(ServiceControllerStatus.Stopped);
sc.Close();
-----------------------------------------------------------------------------------------------------------
[引用Process,設置FileName為sc,引用參數,啟動process來對服務做操作]
using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
var startInfo = process.StartInfo;
startInfo.FileName = "sc";
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.Arguments = string.Format("failure \"{0}\" reset= 0 actions= \"\"/60000", "wuauserv");
//startInfo.Arguments = string.Format("config \"{0}\" start= disabled", "wuauserv");
process.Start();
process.WaitForExit();
}
截自參考
Service Controller
SC指令
SC的問答1
SC的問答2
留言