• C#开发Windows服务的基础代码


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.Configuration.Install;
    using System.Collections.Specialized;
    using System.ServiceProcess;
    
    namespace IAU.ServerInstall.BLL
    {
        public class ServiceControl
        {
            /// <summary>
            /// 注册服务(注册完就启动,已经存在的服务直接启动。)
            /// </summary>
            /// <param name="strServiceName">服务名称</param>
            /// <param name="strServiceInstallPath">服务安装程序完整路径(.exe程序完整路径)</param>
            public void Register(string strServiceName, string strServiceInstallPath)
            {
                IDictionary mySavedState = new Hashtable();
    
                try
                {
                    System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(strServiceName);
    
                    //服务已经存在则卸载
                    if (ServiceIsExisted(strServiceName))
                    {
                        //StopService(strServiceName);
                        UnInstallService(strServiceName, strServiceInstallPath);
                    }
                    service.Refresh();
                    //注册服务 http://www.cnblogs.com/sosoft/
                    AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
    
                    mySavedState.Clear();
                    myAssemblyInstaller.Path = strServiceInstallPath;
                    myAssemblyInstaller.UseNewContext = true;
                    myAssemblyInstaller.Install(mySavedState);
                    myAssemblyInstaller.Commit(mySavedState);
                    myAssemblyInstaller.Dispose();
    
                    service.Start();
                }
                catch (Exception ex)
                {
                    throw new Exception("注册服务时出错:" + ex.Message);
                }
            }
    
            /// <summary>
            /// 卸载服务
            /// </summary>
            /// <param name="strServiceName">服务名称</param>
            /// <param name="strServiceInstallPath">服务安装程序完整路径(.exe程序完整路径)</param>
            public void UnInstallService(string strServiceName, string strServiceInstallPath)
            {
                try
                {
                    if (ServiceIsExisted(strServiceName))
                    {
                        //UnInstall Service
                        AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                        myAssemblyInstaller.UseNewContext = true;
                        myAssemblyInstaller.Path = strServiceInstallPath;
                        myAssemblyInstaller.Uninstall(null);
                        myAssemblyInstaller.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("卸载服务时出错:" + ex.Message);
                }
            }
    
    
            /// <summary>
            /// 判断服务是否存在
            /// </summary>
            /// <param name="serviceName">服务名</param>
            /// <returns></returns>
            public bool ServiceIsExisted(string serviceName)
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController s in services)
                {
                    if (s.ServiceName == serviceName)
                    {
                        return true;
                    }
                }
                return false;
            }
    
            /// <summary>
            /// 启动服务(启动存在的服务,30秒后启动失败报错)
            /// </summary>
            /// <param name="serviceName">服务名</param>
            public void StartService(string serviceName)
            {
                if (ServiceIsExisted(serviceName))
                {
                    System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                    if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                    {
                        service.Start();
                        for (int i = 0; i < 30; i++)
                        {
                            service.Refresh();
                            System.Threading.Thread.Sleep(1000);
                            if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                            {
                                break;
                            }
                            if (i == 29)
                            {
                                throw new Exception("服务" + serviceName + "启动失败!");
                            }
                        }
                    }
                }
            }
    
            /// <summary>
            /// 停止服务(停止存在的服务,30秒后停止失败报错)
            /// </summary>
            /// <param name="serviceName"></param>
            public void StopService(string serviceName)
            {
                if (ServiceIsExisted(serviceName))
                {
                    System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                    if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                    {
                        service.Stop();
                        for (int i = 0; i < 30; i++)
                        {
                            service.Refresh();
                            System.Threading.Thread.Sleep(1000);
                            if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                            {
                                break;
                            }
                            if (i == 29)
                            {
                                throw new Exception("服务" + serviceName + "停止失败!");
                            }
                        }
                    }
                }
            }
        }
    }
    

      

  • 相关阅读:
    AutoFac中常用方法说明
    DI之循环依赖
    NB/T 10215-2019 风力发电机组 测风传感器等最新能源行业标准
    DL/T 691-2019等最新电力行业标准
    TSG D7006-2020 压力管道监督检验规则
    YY/T 0595-2020 医疗器械 质量管理体系YY/T 0287-2017 应用指南
    GB/T 38775-2020系列电动汽车无线充电系统标准
    最新电动汽车安全标准
    GB 38032-2020 电动客车安全要求
    GB 38031-2020 电动汽车用动力蓄电池安全要求
  • 原文地址:https://www.cnblogs.com/answercard/p/3584872.html
Copyright © 2020-2023  润新知