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>配置文件路径</param> /// <param>键值</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 } }