明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 321万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

C#:安装Windows服务,动态指定服务名及描述

Posted on   且行且思  阅读(7370)  评论(1编辑  收藏  举报

Installer.cs>>

复制代码
public Installer()
        {
            InitializeComponent();
            /* 服务未注册前,System.Configuration.ConfigurationManager.AppSettings读取无效。
            //serviceInstaller1.ServiceName = "ChinaHN.XHService." + System.Configuration.ConfigurationManager.AppSettings["Service_ID"];
            //serviceInstaller1.DisplayName = System.Configuration.ConfigurationManager.AppSettings["Service_DisplayName"];
            //serviceInstaller1.Description = System.Configuration.ConfigurationManager.AppSettings["Service_Description"]; 
            */

            /* 指定该服务的启动模式:自动,手动,禁用
            serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.;
            */
            using (SettingHelper setting = new SettingHelper())
            {
                //系统用于标志此服务名称(唯一性)
                serviceInstaller1.ServiceName = setting.ServiceName;
                //向用户标志服务的显示名称(可以重复)
                serviceInstaller1.DisplayName = setting.DisplayName;
                //服务的说明(描述)
                serviceInstaller1.Description = setting.Description;
            } 
        }
复制代码

 

 

配置类:SettingHelper.cs

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System;
using System.IO;
using System.Xml;


//-------------------------------------
// 描述:初始化服务配置帮助类 
// 作者:fooo 
// 完成时间:2013-12-16
//------------------------------------- 
namespace XHService
{
    public class SettingHelper : IDisposable 
    {
        #region 私有成员
        private string _ServiceName;
        private string _DisplayName;
        private string _Description;
        #endregion

        #region 构造函数
        /// <summary> 
        /// 初始化服务配置帮助类 
        /// </summary> 
        public SettingHelper()
        {
            InitSettings();
        }
        #endregion

        #region 属性
        /// <summary> 
        /// 系统用于标志此服务的名称 
        /// </summary> 
        public string ServiceName
        {
            get { return _ServiceName; }
        }
        /// <summary> 
        /// 向用户标志服务的友好名称 
        /// </summary> 
        public string DisplayName
        {
            get { return _DisplayName; }
        }
        /// <summary> 
        /// 服务的说明 
        /// </summary> 
        public string Description
        {
            get { return _Description; }
        }
        #endregion

        #region 私有方法
        #region 初始化服务配置信息
        /// <summary> 
        /// 初始化服务配置信息 
        /// </summary> 
        private void InitSettings()
        {            
            string root = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string xmlfile = root.Remove(root.LastIndexOf('\\') + 1) + "XHService.exe.config";
            if (File.Exists(xmlfile))
            {
                //系统用于标志此服务名称(唯一性)
                _ServiceName = "XHService." + Get_ConfigValue(xmlfile , "Service_ID");
                //向用户标志服务的显示名称(可以重复)
                _DisplayName = Get_ConfigValue(xmlfile, "Service_DisplayName");
                //服务的说明(描述)
                _Description = Get_ConfigValue(xmlfile, "Service_Description");
            }
            else
            {
                throw new FileNotFoundException("未能找到服务名称配置文件 ChinaHN.XHService.exe.config!路径:" + xmlfile);
            }
           
        }
        /// <summary>
        /// 读取 XML中指定节点值
        /// </summary>
        /// <param name="configpath">配置文件路径</param>
        /// <param name="strKeyName">键值</param>        
        /// <returns></returns>
        protected static string Get_ConfigValue(string configpath, string strKeyName)
        {
            using (XmlTextReader tr = new XmlTextReader(configpath))
            {
                while (tr.Read())
                {
                    if (tr.NodeType == XmlNodeType.Element)
                    {
                        if (tr.Name == "add" && tr.GetAttribute("key") == strKeyName)
                        {
                            return tr.GetAttribute("value");
                        }
                    }
                }
            }
            return "";
        }
        #endregion
        #endregion

        #region IDisposable 成员
        private bool disposed = false;
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    //managed dispose 
                    _ServiceName = null;
                    _DisplayName = null;
                    _Description = null;
                }
                //unmanaged dispose 
            }
            disposed = true;
        }
        ~SettingHelper()
        {
            Dispose(false);
        }
        #endregion 
    }
}
复制代码

 

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示