/// <summary> /// 通过键读取配置文件信息 /// </summary> /// <param name="PathMap">自定义属性信息</param> /// <returns>值</returns> private static string GetConfigValue(PathMapAttribute PathMap) { if (!System.IO.File.Exists(ConfigPath)) { return ""; } string path = GetXmlPath(PathMap.Key, PathMap.XmlPath); XmlNode node = null; XmlAttribute attr = null; try { //读取服务器配置文件信息 doc.Load(ConfigPath); node = doc.SelectSingleNode(path); if (node != null) { attr = node.Attributes["value"]; if (attr == null) { throw new Exception("服务器配置文件设置异常,节点" + PathMap.Key + "没有相应的value属性,请检查!"); } return GetRealValue(attr.Value, PathMap.IsDecrypt); } } catch (Exception ex) { throw ex; } return ""; }
/// <summary> /// 初始化配置信息 /// </summary> public static void InitConfig(string logPath) { try { doc = new XmlDocument(); if (!System.IO.File.Exists(ConfigPath)) { return; } Type type = typeof(SysConfig); PropertyInfo[] Props = type.GetProperties(flags); PathMapAttribute PathMap = null; foreach (var prop in Props) { PathMap = GetMyAttribute<PathMapAttribute>(prop, false); if (PathMap != null) { prop.SetValue(null, Convert.ChangeType(GetConfigValue(PathMap), prop.PropertyType), null); } } WriteDeviceLog.WriteLog(logPath, $"配置服务初始化成功!",Guid.NewGuid().ToString()); } catch (Exception ex) { WriteDeviceLog.WriteLog(logPath, $"配置服务初始化错误,原因:{ex}!", Guid.NewGuid().ToString()); } }