判断Windows服务是否启动
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ServiceIsExisted("MSSQLSERVER");
}
private static bool ServiceIsExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == serviceName && s.Status == ServiceControllerStatus.Stopped)
{
s.Start();
return true;
}
}
return false;
}
}
}