public void WriteAppSettings(string sectionName, IDictionary newSettings) { Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ClientSettingsSection section = GetAppConfigSection(appConfig, sectionName, true); if (section == null) { throw new ConfigurationErrorsException("SettingsSaveFailedNoSection"); } SettingElementCollection settings = section.Settings; foreach (DictionaryEntry entry in newSettings) { StoredSetting setting = (StoredSetting)entry.Value; SettingElement element = settings.Get((string)entry.Key); if (element == null) { element = new SettingElement(); element.Name = (string)entry.Key; settings.Add(element); } element.SerializeAs = setting.SerializeAs; element.Value.ValueXml = setting.Value; } try { appConfig.Save(); } catch (ConfigurationErrorsException ex) { throw new ConfigurationErrorsException("SettingsSaveFailed: " + ex.Message, ex); } }
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection values) { IDictionary newConnections = new Hashtable(); IDictionary appSettings = new Hashtable(); IDictionary localSettings = new Hashtable(); foreach (SettingsPropertyValue value in values) { SettingsProperty property = value.Property; if (value.IsDirty) { SpecialSettingAttribute attribute = property.Attributes[typeof(SpecialSettingAttribute)] as SpecialSettingAttribute; if ((attribute != null) && (attribute.SpecialSetting == SpecialSetting.ConnectionString)) { newConnections[property.Name] = value.PropertyValue; } else { StoredSetting setting = new StoredSetting(property.SerializeAs, SerializeToXmlElement(property, value)); if (!IsUserSetting(property)) { appSettings[property.Name] = setting; } else { localSettings[property.Name] = setting; } } value.IsDirty = false; } } string sectionName = GetSectionName(context); if (newConnections.Count > 0) { Store.WriteConnectionStrings(newConnections); } if (localSettings.Count > 0) { Store.WriteUserSettings(sectionName, localSettings); } if (appSettings.Count > 0) { Store.WriteAppSettings(sectionName, appSettings); } }