• C#启动服务


    启动服务的方法有很多种,简单的cmd下dos命名,手动启动,还有C#代码启动。

    我们要实现的功能:

    • 判断是否安装
    • 是否启动
    • 启动服务
    • 关闭服务

    我封装了有关服务的代码,如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace windows服务
    {
        public static class ServicesHelper
        {
            /// <summary>  
            /// 判断是否安装了某个服务  
            /// </summary>  
            /// <param name="serviceName"></param>  
            /// <returns></returns>  
            public static bool ISWindowsServiceInstalled(string serviceName)
            {
                try
                {
                    ServiceController[] services = ServiceController.GetServices();
    
    
                    foreach (ServiceController service in services)
                    {
                        if (service.ServiceName == serviceName)
                        {
                            return true;
                        }
                    }
    
    
                    return false;
                }
                catch
                { return false; }
            }
    
            /// <summary>  
            /// 启动某个服务  
            /// </summary>  
            /// <param name="serviceName"></param>  
            public static void StartService(string serviceName)
            {
                try
                {
                    ServiceController[] services = ServiceController.GetServices();
    
    
                    foreach (ServiceController service in services)
                    {
                        if (service.ServiceName == serviceName)
                        {
                            service.Start();
    
    
                            service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                        }
                    }
                }
                catch { }
            }
    
            /// <summary>  
            /// 停止某个服务  
            /// </summary>  
            /// <param name="serviceName"></param>  
            public static void StopService(string serviceName)
            {
                try
                {
                    ServiceController[] services = ServiceController.GetServices();
    
    
                    foreach (ServiceController service in services)
                    {
                        if (service.ServiceName == serviceName)
                        {
                            service.Stop();
    
    
                            service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                        }
                    }
                }
                catch { }
            }
    
            /// <summary>  
            /// 判断某个服务是否启动  
            /// </summary>  
            /// <param name="serviceName"></param>  
            public static bool ISStart(string serviceName)
            {
                bool result = true;
    
    
                try
                {
                    ServiceController[] services = ServiceController.GetServices();
    
    
                    foreach (ServiceController service in services)
                    {
                        if (service.ServiceName == serviceName)
                        {
                            if ((service.Status == ServiceControllerStatus.Stopped)
                                || (service.Status == ServiceControllerStatus.StopPending))
                            {
                                result = false;
                            }
                        }
                    }
                }
                catch { }
    
    
                return result;
            }
        }
    }
    身是菩提树,心如明镜台,时时勤拂拭,勿使惹尘埃。
  • 相关阅读:
    Google哲学(一)
    Predictably Irractional 相对论的真相
    .NET使用OpenSSL生成的pem密钥文件【做电子商务的朋友可能需要】
    从开辟蓝海到保卫蓝海(一)
    礼让?
    登门槛策略
    从开辟蓝海到保卫蓝海(四)
    盛大招聘 高级数据库开发工程师 工作地点张江高科 学历高者,经验可放宽
    Show一下拿的奖杯
    我们家的一坨和田仔玉[三色皮]
  • 原文地址:https://www.cnblogs.com/birdofparadise/p/7192660.html
Copyright © 2020-2023  润新知