學(xué)習(xí)網(wǎng)址:http://blog.csdn.net/mss359681091/article/details/51073615
主要方法:
啟動某個服務(wù)
停止某個服務(wù)
判斷是否安裝了某個服務(wù)
判斷某個服務(wù)是否啟動
在操作windows服務(wù)之前,先添加引用
System.ServiceProcess
1.判斷是否安裝某服務(wù)
/// <summary>
/// 判斷是否安裝了某個服務(wù)
/// </summary>
/// <param name="serviceName"></param>
/// <returns></returns>
public static bool ISWindowsServiceInstalled(string serviceName)
{
try
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
if (service.ServiceName == serviceName)
{
return true;
}
}
return false;
}
catch
{ return false; }
}
測試:
查看服務(wù)
此時套蒂,我們可以看到MySql是存在的泣懊,也是開啟的……此處只測試開啟的服務(wù)麻惶,關(guān)閉的服務(wù)應(yīng)該是相同的道理窃蹋,偷個懶,就不去測試了匈辱。
在主函數(shù)里面添加使用杀迹,測試結(jié)果
static void Main(string[] args)
{
bool MySqlIsExit = ControlerHelper.ISWindowsServiceInstalled("MySQL");
Console.WriteLine(MySqlIsExit.ToString());
Console.ReadKey();
}
測試結(jié)果
此時的服務(wù)是開啟的树酪,我們先看一下续语,如何關(guān)閉這個服務(wù)……
/// <summary>
/// 停止某個服務(wù)
/// </summary>
/// <param name="serviceName"></param>
public static void StopService(string serviceName)
{
try
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
if (service.ServiceName == serviceName)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
}
}
}
catch { }
}
在主方法里使用測試:
static void Main(string[] args)
{
bool MySqlIsExit = ControlerHelper.ISWindowsServiceInstalled("MySQL");
Console.WriteLine(MySqlIsExit.ToString());
ControlerHelper.StopService("MySQL");
Console.ReadKey();
}
測試關(guān)閉服務(wù)
當(dāng)服務(wù)關(guān)閉時疮茄,如果再次執(zhí)行關(guān)閉操作也并不會報錯力试!
此時的服務(wù)是關(guān)閉的,那接下來看看這個方法畸裳,判斷服務(wù)是否開啟:
/// <summary>
/// 判斷某個服務(wù)是否啟動
/// </summary>
/// <param name="serviceName"></param>
public static bool ISStart(string serviceName)
{
bool result = true;
try
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
if (service.ServiceName == serviceName)
{
if ((service.Status == ServiceControllerStatus.Stopped)
|| (service.Status == ServiceControllerStatus.StopPending))
{
result = false;
}
}
}
}
catch { }
return result;
}
在主方法里使用測試:
static void Main(string[] args)
{
bool MySqlIsExit = ControlerHelper.ISWindowsServiceInstalled("MySQL");
Console.WriteLine(MySqlIsExit.ToString());
ControlerHelper.StopService("MySQL");
bool isStart = ControlerHelper.ISStart("MySQL");
Console.WriteLine("服務(wù)是否啟動:" + isStart);
Console.ReadKey();
}
可見此時的服務(wù)確實是關(guān)閉的民鼓。
image.png
最后我們再寫一個啟動服務(wù)的方法蓬抄,進行測試:
/// <summary>
/// 啟動某個服務(wù)
/// </summary>
/// <param name="serviceName"></param>
public static void StartService(string serviceName)
{
try
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
if (service.ServiceName == serviceName)
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
}
}
}
catch { }
}
在主方法里面把該服務(wù)啟動嚷缭,然后再進行判斷該服務(wù)是否啟動阅爽。
static void Main(string[] args)
{
bool MySqlIsExit = ControlerHelper.ISWindowsServiceInstalled("MySQL");
Console.WriteLine(MySqlIsExit.ToString());
ControlerHelper.StopService("MySQL");
bool isStart = ControlerHelper.ISStart("MySQL");
Console.WriteLine("服務(wù)是否啟動:" + isStart);
ControlerHelper.StartService("MySQL");
bool isStartAgain = ControlerHelper.ISStart("MySQL");
Console.WriteLine("服務(wù)是否啟動:" + isStartAgain);
Console.ReadKey();
}
此處開啟服務(wù)需要一點時間……所以在起初判斷服務(wù)開啟和后來的判斷有一點停頓時間。
測試結(jié)果
好了,練習(xí)到此結(jié)束百侧。