判断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;
}
}
}

 

posted on 2011-12-14 11:10  数据结构X  阅读(295)  评论(0编辑  收藏  举报

导航