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); }
public void DefaultValueIsNull() { var Element = new SettingElement(); Assert.Null(Element.Value.CurrentConfiguration); }
/// <summary> /// Initializes a new instance of the <see cref="SettingElementPropertyDescriptor"/> class. /// </summary> /// <param name="settings">The settings.</param> /// <param name="setting">The setting.</param> public SettingElementPropertyDescriptor(SettingElementCollection settings, SettingElement setting) : base(setting.Key, null) { this.settings = settings; this.setting = setting; }
public void DefaultSettingSerializationIsString() { var Element = new SettingElement(); Assert.Equal(SettingsSerializeAs.String, Element.SerializeAs); }
public void DefaultNameIsEmptyString() { var Element = new SettingElement(); Assert.Equal(string.Empty, Element.Name); }
public void Remove(SettingElement element) { }
internal ConfigSetting(bool isAppSetting, SettingElement setting) { _isAppSetting = isAppSetting; _setting = setting; }
public void Remove(SettingElement element) { throw new NotImplementedException(); }
public object this[Type type, string key] { get { if (this.ContainsSection(type) == false) { return(null); } var section = this.GetSection(type); var element = section.Settings.Get(key); if (element == null) { return(null); } var valueXml = element.Value.ValueXml; var typeValue = valueXml.Attributes["type"]; var valueType = typeValue == null ? typeof(string) : Type.GetType(typeValue.Value); if (element.SerializeAs == SettingsSerializeAs.String) { var converter = TypeDescriptor.GetConverter(valueType); return(converter.ConvertFromString(element.Value.ValueXml.InnerText)); } if (typeof(IXmlSerializable).IsAssignableFrom(valueType) == true) { return(element.Value.ValueXml.InnerXml); } return(XmlSerializerUtility.ReadString(element.Value.ValueXml.InnerXml, valueType)); } set { var section = this.GetSection(type); var element = section.Settings.Get(key); if (value == null && element != null) { section.Settings.Remove(element); return; } if (element == null) { element = new SettingElement(key, SettingsSerializeAs.String); section.Settings.Add(element); } var doc = new XmlDocument(); var textNode = doc.CreateElement("value"); doc.AppendChild(textNode); var converter = TypeDescriptor.GetConverter(value); if (converter.CanConvertFrom(typeof(string)) == true) { var textValue = converter.ConvertToString(value); if (textValue != string.Empty) { textNode.AppendChild(doc.CreateTextNode(textValue)); } element.SerializeAs = SettingsSerializeAs.String; } else if (value is IXmlSerializable) { var s = value as IXmlSerializable; var settings = new XmlWriterSettings() { Indent = true, Encoding = Encoding.UTF8, OmitXmlDeclaration = true }; using (var sw = new Utf8StringWriter()) using (var writer = XmlWriter.Create(sw, settings)) { s.WriteXml(writer); writer.Close(); textNode.InnerXml = sw.ToString(); element.SerializeAs = SettingsSerializeAs.Xml; } } else { textNode.InnerXml = XmlSerializerUtility.GetString(value, true, true); element.SerializeAs = SettingsSerializeAs.Xml; } if (value is string == false) { var attribute = doc.CreateAttribute("type"); attribute.Value = value.GetType().AssemblyQualifiedName; textNode.Attributes.Append(attribute); } element.Value = new SettingValueElement() { ValueXml = textNode }; ConfigurationManager.RefreshSection(section.SectionInformation.SectionName); } }
public void Add(SettingElement element) { BaseAdd(element); }
public void Remove(SettingElement element) { BaseRemove(GetElementKey(element)); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Sets the values of the specified group of property settings. /// </summary> /// <param name="context">A <see cref="T:System.Configuration.SettingsContext"/> /// describing the current application usage.</param> /// <param name="values">A <see cref="T:System.Configuration.SettingsPropertyValueCollection"/> /// representing the group of property settings to set.</param> /// <exception cref="T:System.Configuration.ConfigurationErrorsException">A user-scoped /// setting was encountered but the current configuration only supports application- /// scoped settings. /// -or-There was a general failure saving the settings to the configuration file. /// </exception> /// <PermissionSet> /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /// version="1" Unrestricted="true"/> /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /// version="1" Unrestricted="true"/> /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /// version="1" Flags="ControlEvidence, ControlPrincipal"/> /// </PermissionSet> /// ------------------------------------------------------------------------------------ public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection values) { // Set the config files ExeConfigurationFileMap configMap = SetConfigFiles(); Configuration localConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.PerUserRoamingAndLocal); Configuration roamingConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.PerUserRoaming); string groupName = (string)context["GroupName"]; ClientSettingsSection localSettings = localConfig.GetSectionGroup("userSettings").Sections[groupName] as ClientSettingsSection; ClientSettingsSection roamingSettings = roamingConfig.GetSectionGroup("userSettings").Sections[groupName] as ClientSettingsSection; SettingElementCollection localCollection = localSettings.Settings; SettingElementCollection roamingCollection = roamingSettings.Settings; // Create new collection of values foreach (SettingsPropertyValue value in values) { if (value.Property.Attributes[typeof(UserScopedSettingAttribute)] != null) { SettingElement elem; if (value.Property.Attributes[typeof(SettingsManageabilityAttribute)] == null) { // this is a property for a local user elem = localCollection.Get(value.Name); if (elem == null) { elem = new SettingElement(); elem.Name = value.Name; localCollection.Add(elem); } } else { // this is a property for a roaming user elem = roamingCollection.Get(value.Name); if (elem == null) { elem = new SettingElement(); elem.Name = value.Name; roamingCollection.Add(elem); } } elem.SerializeAs = value.Property.SerializeAs; elem.Value.ValueXml = SerializeToXmlElement(value); } } if (localCollection.Count > 0) { localConfig.Save(); } if (roamingCollection.Count > 0) { roamingConfig.Save(); } }
public void Add(SettingElement element) { }
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); } } } }
/// <summary> /// need to execute from Synchronized method /// </summary> /// <param name="keyName"></param> /// <returns></returns> private string GetValueImple(string keyName) { if (configValueMap.ContainsKey(keyName)) { return((string)configValueMap[keyName]); } Configuration config = null; try { Assembly ass = Assembly.GetEntryAssembly(); if (ass != null) { config = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location); } else { config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); SECTION_GROUP_NAME = "applicationSettings"; } } catch (ConfigurationErrorsException Ex) { //implement system exception MessageData messageData = new MessageData("ffce00003", Properties.Resources.ffce00003, Ex.Message); logger.Error(messageData, Ex); throw new SystemException(messageData); } if (!config.HasFile) { //implement system exception MessageData messageData = new MessageData("ffce00004", Properties.Resources.ffce00004); logger.Error(messageData); throw new SystemException(messageData); } ConfigurationSectionGroup sectionGroup = config.SectionGroups[SECTION_GROUP_NAME]; if (sectionGroup == null) { //implement system exception MessageData messageData = new MessageData("ffce00005", Properties.Resources.ffce00005); logger.Error(messageData); throw new SystemException(messageData); } string value = null; foreach (ConfigurationSection configurationSection in sectionGroup.Sections) { ClientSettingsSection section = null; try { section = (ClientSettingsSection)ConfigurationManager .GetSection(configurationSection.SectionInformation.SectionName); } catch (ConfigurationErrorsException ex) { //implement system exception MessageData messageData = new MessageData("ffce00006", Properties.Resources.ffce00006, ex.Message); logger.Error(messageData, ex); throw new SystemException(messageData); } SettingElement element = section.Settings.Get(keyName); if (element == null) { //implement system exception MessageData messageData = new MessageData("ffce00007", Properties.Resources.ffce00007, keyName); logger.Error(messageData); throw new SystemException(messageData); } value = element.Value.ValueXml.InnerText; break; } //set to cache configValueMap.Add(keyName, value); return(value); }
} // 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
/// <summary> /// get and return the value of the configuration settings for keyname /// </summary> /// <param name="config">Configuration of the assembly</param> /// <param name="keyName">keyName to be found from the settings file</param> /// <exception cref="System.NullReferenceException">if keyname not found in the configuration file</exception> /// <exception cref="System.NullReferenceException">if DEFAULT_APPLICATION_ENVIRONMENT_SETTINGS not found in the configuration file</exception> /// <exception cref="System.NullReferenceException">if element is not found </exception> /// <exception cref="System.NullReferenceException">if value is not found </exception> /// <returns>return the value in the configuration for the keyname</returns> private string GetConfigurationValue(Configuration config, string keyName) { //userSettings for windows application settings/applicationSettings for webservices ConfigurationSectionGroup sectionGroup = config.SectionGroups[SECTION_GROUP_NAME]; if (sectionGroup == null) // failed to get sectiongroup { //implement system exception MessageData messageData = new MessageData("ffce00005", Properties.Resources.ffce00005); logger.Error(messageData, new NullReferenceException()); throw new SystemException(messageData, new NullReferenceException()); } //read default settings file first SettingElement element = GetConfigurationElement(sectionGroup, DEFAULT_SETTINGS_NAME, keyName); if (element == null) // if element not found in default settings file then get from applicatoin setting file { string applicationSettingsFileName = configValueMap.ContainsKey(APPLICATION_ENVIRONMENT_SETTINGS) ? (string)configValueMap[APPLICATION_ENVIRONMENT_SETTINGS] : null; if (applicationSettingsFileName == null) //if APPLICATION_ENVIRONMENT_SETTINGS not found in cache memory throw exception { //temporary solution to get APPLICATION_ENVIRONMENT_SETTINGS applicationSettingsFileName = GetConfigurationValue(config, APPLICATION_ENVIRONMENT_SETTINGS); if (applicationSettingsFileName == null) { //implement system exception MessageData messageData = new MessageData("ffce00030", Properties.Resources.ffce00030, keyName); logger.Error(messageData, new NullReferenceException()); throw new SystemException(messageData, new NullReferenceException()); } } element = GetConfigurationElement(sectionGroup, applicationSettingsFileName, keyName); } if (element == null) // failed to get element { //implement system exception MessageData messageData = new MessageData("ffce00036", Properties.Resources.ffce00036, keyName); logger.Error(messageData, new NullReferenceException()); throw new SystemException(messageData, new NullReferenceException()); } //get the exact value from innertext of the element string value = element.Value.ValueXml.InnerText; if (value == null) //value is not available for the keyname { //implement system exception MessageData messageData = new MessageData("ffce00034", Properties.Resources.ffce00034, keyName); logger.Error(messageData, new NullReferenceException()); throw new SystemException(messageData, new NullReferenceException()); } return(value); }