public void EditBeforeAdd() { UserSettingsGroup u = new UserSettingsGroup(); ClientSettingsSection c = new ClientSettingsSection(); u.Sections.Add("mine", c); }
public void EditBeforeAdd() { UserSettingsGroup u = new UserSettingsGroup(); ClientSettingsSection c = new ClientSettingsSection(); Assert.Throws <InvalidOperationException>(() => u.Sections.Add("mine", c)); }
private void WriteSettings(string sectionName, IEnumerable <SettingElement> settingElements) { var userConfig = OpenConfigurationFile(); var section = GetUserSettingsSection(sectionName, userConfig); if (section == null) { var userSettingsSectionGroup = (UserSettingsGroup)userConfig.SectionGroups["userSettings"]; if (userSettingsSectionGroup == null) { userSettingsSectionGroup = new UserSettingsGroup(); userConfig.SectionGroups.Add("userSettings", userSettingsSectionGroup); } section = new ClientSettingsSection(); userSettingsSectionGroup.Sections.Add(sectionName, section); } var settings = section.Settings; foreach (var element in settingElements) { settings.Add(element); } userConfig.Save(); }
public IConfigurationSectionGroup AddSectionGroup(string sectionGroupName) { UserSettingsGroup userGroup = new UserSettingsGroup(); _configuration.SectionGroups.Add(sectionGroupName, userGroup); return(new ConfigurationSectionGroupWrapper(userGroup)); }
} // UserSettingsGroup // ---------------------------------------------------------------------- public void RemoveAllSections() { UserSettingsGroup userSettingsGroup = UserSettingsGroup; if (userSettingsGroup == null) { return; } configuration.SectionGroups.Remove(userSettingsGroup.SectionGroupName); } // RemoveAllSections
public void EditAfterAdd() { Config cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); UserSettingsGroup u = new UserSettingsGroup(); cfg.SectionGroups.Add("userSettings", u); ClientSettingsSection c = new ClientSettingsSection(); u.Sections.Add("mine", c); }
} // RemoveSection // ---------------------------------------------------------------------- public bool HasSameSettings(UserConfig compareUserConfig) { if (compareUserConfig == null) { throw new ArgumentNullException("compareUserConfig"); } if (configuration.SectionGroups.Count != compareUserConfig.configuration.SectionGroups.Count) { return(false); } foreach (ConfigurationSectionGroup compareSectionGroup in compareUserConfig.configuration.SectionGroups) { UserSettingsGroup compareUserSettingsGroup = compareSectionGroup as UserSettingsGroup; if (compareUserSettingsGroup == null) { continue; } UserSettingsGroup userSettingsGroup = configuration.SectionGroups[compareSectionGroup.Name] as UserSettingsGroup; if (userSettingsGroup == null || userSettingsGroup.Sections.Count != compareSectionGroup.Sections.Count) { return(false); } foreach (ConfigurationSection compareSection in compareSectionGroup.Sections) { ClientSettingsSection compareClientSettingsSection = compareSection as ClientSettingsSection; if (compareClientSettingsSection == null) { continue; } ClientSettingsSection clientSettingsSection = userSettingsGroup.Sections[compareSection.SectionInformation.Name] as ClientSettingsSection; if (clientSettingsSection == null || clientSettingsSection.Settings.Count != compareClientSettingsSection.Settings.Count) { return(false); } foreach (SettingElement compateSettingElement in compareClientSettingsSection.Settings) { SettingElement settingElement = clientSettingsSection.Settings.Get(compateSettingElement.Name); if (settingElement == null || !settingElement.Value.ValueXml.InnerXml.Equals(compateSettingElement.Value.ValueXml.InnerXml)) { return(false); } } } } return(true); } // HasSameSettings
static ConfigurationSectionGroup AddSectionGroup(Configuration pConfiguration, String pSectionGroupName) { ConfigurationSectionGroup wConfigurationSectionGroup = null; if (pSectionGroupName == "applicationSettings") { wConfigurationSectionGroup = new ApplicationSettingsGroup(); } if (pSectionGroupName == "userSettingsGroup") { wConfigurationSectionGroup = new UserSettingsGroup(); } pConfiguration.SectionGroups.Add(pSectionGroupName, wConfigurationSectionGroup); return(wConfigurationSectionGroup); }
/// <summary> /// Gets the <see cref="SettingElementCollection"/> for the specified section name within /// the specified configuration. /// </summary> /// <param name="config">The <see cref="Configuration"/> object.</param> /// <param name="sectionName">The settings section name.</param> /// <returns> /// A <see cref="SettingElementCollection"/> for the section, or an empty section if not found. /// </returns> private static SettingElementCollection GetSettingElementCollection(Configuration config, string sectionName) { var userSettings = config.GetSectionGroup("userSettings"); if (userSettings == null) { userSettings = new UserSettingsGroup(); config.SectionGroups.Add("userSettings", userSettings); } if (!(userSettings.Sections.Get(sectionName) is ClientSettingsSection section)) { section = new ClientSettingsSection(); userSettings.Sections.Add(sectionName, section); } return(section.Settings); }
ClientSettingsSection GetConfigurationSection() { //string targetDir = (string)Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Outlook\Addins\FonlowSyncML.Addin", "TargetDir", String.Empty); //if (String.IsNullOrEmpty(targetDir)) //{ // Trace.TraceWarning("Cannot locate TargetDir in key FonlowSyncML.Addin, and the plugin will might not be loaded properly."); // return null; //} ConfigurationFileMap fileMap = new ConfigurationFileMap(UserConfigFilePath); System.Configuration.Configuration config = ConfigurationManager.OpenMappedMachineConfiguration(fileMap); UserSettingsGroup userSectionGroup = config.SectionGroups["userSettings"] as UserSettingsGroup; if (userSectionGroup == null) { Trace.TraceWarning("No userSettings found in {0}.", UserConfigFilePath); return(null); } return(userSectionGroup.Sections[SectionName] as ClientSettingsSection); }
} // RemoveAllSections // ---------------------------------------------------------------------- public void RemoveSection(string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } UserSettingsGroup userSettingsGroup = UserSettingsGroup; if (userSettingsGroup == null) { return; } ConfigurationSection section = userSettingsGroup.Sections[name]; if (section == null) { throw new InvalidOperationException("invalid section " + name); } userSettingsGroup.Sections.Remove(name); } // RemoveSection
} // SaveAs // ---------------------------------------------------------------------- public void Import(UserConfig importUserConfig, bool overwriteSettings) { if (importUserConfig == null) { throw new ArgumentNullException("importUserConfig"); } foreach (ConfigurationSectionGroup importSectionGroup in importUserConfig.configuration.SectionGroups) { UserSettingsGroup importUserSettingsGroup = importSectionGroup as UserSettingsGroup; if (importUserSettingsGroup == null) { continue; } UserSettingsGroup userSettingsGroup = configuration.SectionGroups[importSectionGroup.Name] as UserSettingsGroup; if (userSettingsGroup == null) { userSettingsGroup = new UserSettingsGroup(); configuration.SectionGroups.Add(importSectionGroup.Name, userSettingsGroup); } foreach (ConfigurationSection importSection in importSectionGroup.Sections) { ClientSettingsSection importClientSettingsSection = importSection as ClientSettingsSection; if (importClientSettingsSection == null) { continue; } ClientSettingsSection clientSettingsSection = userSettingsGroup.Sections[importSection.SectionInformation.Name] as ClientSettingsSection; if (clientSettingsSection == null) { clientSettingsSection = new ClientSettingsSection(); userSettingsGroup.Sections.Add(importSection.SectionInformation.Name, clientSettingsSection); } foreach (SettingElement importSettingElement in importClientSettingsSection.Settings) { bool newSetting = false; SettingElement settingElement = clientSettingsSection.Settings.Get(importSettingElement.Name); if (settingElement == null) { newSetting = true; settingElement = new SettingElement(); settingElement.Name = importSettingElement.Name; settingElement.SerializeAs = importSettingElement.SerializeAs; clientSettingsSection.Settings.Add(settingElement); } if (!newSetting && !overwriteSettings) { continue; } SettingValueElement settingValueElement = new SettingValueElement(); settingValueElement.ValueXml = importSettingElement.Value.ValueXml; settingElement.SerializeAs = importSettingElement.SerializeAs; settingElement.Value = settingValueElement; clientSettingsSection.Settings.Add(settingElement); } } } } // Import
public void Import(UserConfig importUserConfig, bool overwriteSettings) { if (importUserConfig == null) { throw new ArgumentNullException("importUserConfig"); } foreach (ConfigurationSectionGroup _importSectionGroup in importUserConfig.m_configuration.SectionGroups) { UserSettingsGroup _importUserSettingsGroup = _importSectionGroup as UserSettingsGroup; if (_importUserSettingsGroup == null) { continue; } UserSettingsGroup _userSettingsGroup = m_configuration.SectionGroups[_importSectionGroup.Name] as UserSettingsGroup; if (_userSettingsGroup == null) { _userSettingsGroup = new UserSettingsGroup(); m_configuration.SectionGroups.Add(_importSectionGroup.Name, _userSettingsGroup); } foreach (ConfigurationSection importSection in _importSectionGroup.Sections) { ClientSettingsSection _importClientSettingsSection = importSection as ClientSettingsSection; if (_importClientSettingsSection == null) { continue; } ClientSettingsSection _clientSettingsSection = _userSettingsGroup.Sections[importSection.SectionInformation.Name] as ClientSettingsSection; if (_clientSettingsSection == null) { _clientSettingsSection = new ClientSettingsSection(); _userSettingsGroup.Sections.Add(importSection.SectionInformation.Name, _clientSettingsSection); } foreach (SettingElement _importSettingElement in _importClientSettingsSection.Settings) { bool _newSetting = false; SettingElement _settingElement = _clientSettingsSection.Settings.Get(_importSettingElement.Name); if (_settingElement == null) { _newSetting = true; _settingElement = new SettingElement(); _settingElement.Name = _importSettingElement.Name; _settingElement.SerializeAs = _importSettingElement.SerializeAs; _clientSettingsSection.Settings.Add(_settingElement); } if (!_newSetting && !overwriteSettings) { continue; } SettingValueElement _settingValueElement = new SettingValueElement(); _settingValueElement.ValueXml = _importSettingElement.Value.ValueXml; _settingElement.SerializeAs = _importSettingElement.SerializeAs; _settingElement.Value = _settingValueElement; _clientSettingsSection.Settings.Add(_settingElement); } } } }
public bool HasSameSettings(UserConfig compareUserConfig) { if (compareUserConfig == null) { throw new ArgumentNullException("compareUserConfig"); } if (m_configuration.SectionGroups.Count != compareUserConfig.m_configuration.SectionGroups.Count) { return(false); } foreach (ConfigurationSectionGroup _compareSectionGroup in compareUserConfig.m_configuration.SectionGroups) { UserSettingsGroup _compareUserSettingsGroup = _compareSectionGroup as UserSettingsGroup; if (_compareUserSettingsGroup == null) { continue; } UserSettingsGroup _userSettingsGroup = m_configuration.SectionGroups[_compareSectionGroup.Name] as UserSettingsGroup; if (_userSettingsGroup == null || _userSettingsGroup.Sections.Count != _compareSectionGroup.Sections.Count) { return(false); } foreach (ConfigurationSection _compareSection in _compareSectionGroup.Sections) { ClientSettingsSection _compareClientSettingsSection = _compareSection as ClientSettingsSection; if (_compareClientSettingsSection == null) { continue; } ClientSettingsSection _clientSettingsSection = _userSettingsGroup.Sections[_compareSection.SectionInformation.Name] as ClientSettingsSection; if (_clientSettingsSection == null || _clientSettingsSection.Settings.Count != _compareClientSettingsSection.Settings.Count) { return(false); } foreach (SettingElement _compateSettingElement in _compareClientSettingsSection.Settings) { SettingElement _settingElement = _clientSettingsSection.Settings.Get(_compateSettingElement.Name); if (_settingElement == null || !_settingElement.Value.ValueXml.InnerXml.Equals(_compateSettingElement.Value.ValueXml.InnerXml)) { return(false); } } } } return(true); }