c# 判断Window服务是否存在
1. 操作WINDOW服务需要在C#里引入一个类库:System.ServiceProcess
//监测服务是否启动
private bool ServicesExists(string serviceName)
{
bool isbn = false;
//获取所有服务
ServiceController[] services = ServiceController.GetServices();
try
{
foreach (ServiceController service in services)
{
if (service.ServiceName.ToUpper() == serviceName.ToUpper())
{
isbn = true;
break;
}
}
return isbn;
}
catch (Exception ex)
{
return false;
}
}
//启动服务
private bool ServiceStar(string serviceName)
{
bool isbn = false;
try
{
if (ServicesExists(serviceName))
{
ServiceController star_service = new ServiceController(serviceName);
if (star_service.Status != ServiceControllerStatus.Running &&
star_service.Status != ServiceControllerStatus.StartPending)
{
star_service.Start();
for (int i = 0; i < 60; i++)
{
star_service.Refresh();
System.Threading.Thread.Sleep(1000);
if (star_service.Status == ServiceControllerStatus.Running)
&nbs