masterfy

 

判断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 2010-04-24 11:28  masterfy  阅读(529)  评论(0编辑  收藏  举报

导航