/// <summary> /// 从SectionGroup中读取Section。在Section写在Group里时使用 /// </summary> /// <param name="sectionName">section name</param> /// <param name="groups">SectionGroup</param> /// <returns>ConfigurationSection</returns> private static ConfigurationSection GetSectionFromGroups(string sectionName, ConfigurationSectionGroupCollection groups) { ConfigurationSection section = null; for (int i = 0; i < groups.Count; i++) { try { ConfigurationSectionGroup group = groups[i]; if (group.SectionGroups.Count > 0) section = GetSectionFromGroups(sectionName, group.SectionGroups); else section = group.Sections[sectionName]; if (section != null) break; } catch (System.IO.FileNotFoundException) { } } return section; }
public SettingsReader(string settingsXML) { if (File.Exists(settingsXML)) { fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = settingsXML; config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); sectionGroups = config.SectionGroups; } else { throw new Exception("An error occurred while trying to read stored settings." + Environment.NewLine + "The configuration file: " + settingsXML + " was not found." + Environment.NewLine + "Error CNF-336 in " + PROJ_FILE_NAME + ".SettingsReader()"); } }