首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# 判断Window服务是不是存在

2012-08-31 
c# 判断Window服务是否存在1.操作WINDOW服务需要在C#里引入一个类库:System.ServiceProcess//监测服务是否

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)
                        {
                            isbn = true;
                            break;
                        }
                        if (i == 59)
                        {
                            isbn = false;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            return false;
        }
        return isbn;
    }

    //停止服务
    private bool ServiceStop(string serviceName)
    {
        bool isbn = false;

        try
        {
            if (ServicesExists(serviceName))
            {
                ServiceController star_service = new ServiceController(serviceName);
                if (star_service.Status == ServiceControllerStatus.Running)
                {
                    star_service.Stop();

                    for (int i = 0; i < 60; i++)
                    {
                        star_service.Refresh();
                        System.Threading.Thread.Sleep(1000);
                        if (star_service.Status == ServiceControllerStatus.Stopped)
                        {
                            isbn = true;
                            break;
                        }
                        if (i == 59)
                        {
                            isbn = false;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            return false;
        }
        return isbn;
    }

http://bbs.itzk.com/thread-2617-1-1.html

热点排行