判断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;
}
}
}
原文地址:https://www.cnblogs.com/ToddLai/p/2287324.html