public MisfitConfiguration() { this.SectionHandler = (MisfitSectionHandler)ConfigurationManager.GetSection(MisfitSectionHandler.DefaultSectionName); if (this.SectionHandler == null) { throw new ConfigurationErrorsException(String.Format("没有到对应的配置元素节点{0}", MisfitSectionHandler.DefaultSectionName)); } }
/// <summary> /// 解析对应配置文件的配置信息 /// </summary> /// <param name="reader"></param> /// <returns></returns> public static MisfitSectionHandler Deserialize(XmlReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } reader.MoveToContent(); if (reader.EOF) { throw new ConfigurationErrorsException(); } var handler = new MisfitSectionHandler(); handler.DeserializeElement(reader, false); return(handler); }
/// <summary> /// 解析对应配置文件的配置信息 /// </summary> /// <param name="configurationFile"></param> /// <param name="configurationSection"></param> /// <returns></returns> public static MisfitSectionHandler Deserialize(string configurationFile) { if (string.IsNullOrWhiteSpace(configurationFile)) { throw new ArgumentNullException("configurationFile"); } if (!Path.IsPathRooted(configurationFile)) { var configurationDirectory = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); configurationFile = Path.Combine(configurationDirectory, configurationFile); } if (!File.Exists(configurationFile)) { throw new FileNotFoundException(string.Format("没有找到对应的{0}配置文件", configurationFile)); } ExeConfigurationFileMap exeConfigurationFileMap = new ExeConfigurationFileMap(); exeConfigurationFileMap.ExeConfigFilename = configurationFile; System.Configuration.Configuration configuration = null; try { configuration = ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap, ConfigurationUserLevel.None); } catch (ConfigurationErrorsException) { using (var reader = new XmlTextReader(File.OpenRead(configurationFile))) { return(MisfitSectionHandler.Deserialize(reader)); } } var handler = (MisfitSectionHandler)configuration.GetSection(DefaultSectionName); if (handler == null) { throw new ConfigurationErrorsException(string.Format("没有找到对应的 {0} 根节点", DefaultSectionName)); } return(handler); }
public MisfitConfiguration(string configurationFile) { this.SectionHandler = MisfitSectionHandler.Deserialize(configurationFile); }