using System; using System.IO; using System.Xml; namespace HL_FristAidPlatform_POCTWindowsService { public class Config : IDisposable { #region 属性 /// /// 系统用于标志此服务的名称 /// public string ServiceName; /// /// 向用户标志服务的友好名称 /// public string DisplayName; /// /// 服务的说明 /// public string ServiceDescription; /// /// 监听端口 /// public int StrPort; /// /// 是否开启调试模式 True:开启,记录错误日志;False:不开启,日常运行 /// public bool IsDebug; /// /// 数据库连接字符 /// public string StrCon; /// /// 胸痛中心系统 模块编号 /// public string ChestPain; /// /// 卒中中心系统 模块编号 /// public string Apoplexy; /// /// 创伤救治中心系统 模块编号 /// public string Trauma; /// /// 危重孕产妇救治中心系统 模块编号 /// public string CriticalPregnant; /// /// 危重新生儿救治中心系统 模块编号 /// public string CriticalNeonatal; #endregion #region 构造函数 /// /// 初始化服务配置帮助类 /// public Config() { InitSettings(); } #endregion #region 私有方法 #region 初始化服务配置信息 /// /// 初始化服务配置信息 /// private void InitSettings() { string root = System.Reflection.Assembly.GetExecutingAssembly().Location; string xmlfile = root.Remove(root.LastIndexOf('\\') + 1) + "HL_FristAidPlatform_POCTWindowsService.exe.config"; if (File.Exists(xmlfile)) { //系统用于标志此服务名称(唯一性) ServiceName = Get_ConfigValue(xmlfile, "ServiceName"); //向用户标志服务的显示名称(可以重复) DisplayName = Get_ConfigValue(xmlfile, "DisplayName"); //服务的说明(描述) ServiceDescription = Get_ConfigValue(xmlfile, "ServiceDescription"); //监听端口 StrPort = Convert.ToInt32(Get_ConfigValue(xmlfile, "StrPort")); //是否开启调试模式 True:开启,记录错误日志;False:不开启,日常运行 IsDebug = Convert.ToBoolean(Get_ConfigValue(xmlfile, "IsDebug")); //数据库连接字符 StrCon = Get_ConfigValue(xmlfile, "StrCon"); //胸痛中心系统 模块编号 ChestPain = Get_ConfigValue(xmlfile, "ChestPain"); //卒中中心系统 模块编号 Apoplexy = Get_ConfigValue(xmlfile, "Apoplexy"); //创伤救治中心系统 模块编号 Trauma = Get_ConfigValue(xmlfile, "Trauma"); //危重孕产妇救治中心系统 模块编号 CriticalPregnant = Get_ConfigValue(xmlfile, "CriticalPregnant"); //危重新生儿救治中心系统 模块编号 CriticalNeonatal = Get_ConfigValue(xmlfile, "CriticalNeonatal"); } else { throw new FileNotFoundException("未能找到服务名称配置文件 HL_FristAidPlatform_POCTWindowsService.exe.config!路径:" + xmlfile); } } /// /// 读取 XML中指定节点值 /// /// 配置文件路径 /// 键值 /// 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; ServiceDescription = null; } //unmanaged dispose } disposed = true; } ~Config() { Dispose(false); } #endregion } }