C#操作Windows服务的一些方法
2017-11-12 本文已影响86人
张中华
学习网址:http://blog.csdn.net/mss359681091/article/details/51073615
主要方法:
启动某个服务
停止某个服务
判断是否安装了某个服务
判断某个服务是否启动
在操作windows服务之前,先添加引用
System.ServiceProcess
1.判断是否安装某服务
/// <summary>
/// 判断是否安装了某个服务
/// </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; }
}
测试:

此时,我们可以看到MySql是存在的,也是开启的……此处只测试开启的服务,关闭的服务应该是相同的道理,偷个懒,就不去测试了。

在主函数里面添加使用,测试结果
static void Main(string[] args)
{
bool MySqlIsExit = ControlerHelper.ISWindowsServiceInstalled("MySQL");
Console.WriteLine(MySqlIsExit.ToString());
Console.ReadKey();
}

此时的服务是开启的,我们先看一下,如何关闭这个服务……
/// <summary>
/// 停止某个服务
/// </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();
}

当服务关闭时,如果再次执行关闭操作也并不会报错!
此时的服务是关闭的,那接下来看看这个方法,判断服务是否开启:
/// <summary>
/// 判断某个服务是否启动
/// </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("服务是否启动:" + isStart);
Console.ReadKey();
}
可见此时的服务确实是关闭的。

最后我们再写一个启动服务的方法,进行测试:
/// <summary>
/// 启动某个服务
/// </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 { }
}
在主方法里面把该服务启动,然后再进行判断该服务是否启动。
static void Main(string[] args)
{
bool MySqlIsExit = ControlerHelper.ISWindowsServiceInstalled("MySQL");
Console.WriteLine(MySqlIsExit.ToString());
ControlerHelper.StopService("MySQL");
bool isStart = ControlerHelper.ISStart("MySQL");
Console.WriteLine("服务是否启动:" + isStart);
ControlerHelper.StartService("MySQL");
bool isStartAgain = ControlerHelper.ISStart("MySQL");
Console.WriteLine("服务是否启动:" + isStartAgain);
Console.ReadKey();
}
此处开启服务需要一点时间……所以在起初判断服务开启和后来的判断有一点停顿时间。

好了,练习到此结束。