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 IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端