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 void CollectionAddNameless() { SettingElement el = new SettingElement(); Assert.AreEqual(String.Empty, el.Name, "premise #1"); SettingElementCollection c = new SettingElementCollection(); Assert.AreEqual(ConfigurationElementCollectionType.BasicMap, c.CollectionType, "premise #2"); c.Add(el); Assert.AreEqual(el, c.Get(""), "#1"); }
public void CollectionAddNull() { try { SettingElementCollection c = new SettingElementCollection(); c.Add(null); Assert.Fail(); } catch (NullReferenceException) { // .net s cks here } catch (ArgumentNullException) { } }
/// <summary> /// Checks the specified settings collection to see if it has a serialized value that should /// be applied to the specified <see cref="SettingsPropertyValue"/>. /// </summary> /// <param name="value">An individual settings property value.</param> /// <param name="settings">A collection representing the settings.</param> private static void ApplySettingToValue(SettingsPropertyValue value, SettingElementCollection settings) { var setting = settings.Get(value.Name); if (setting != null) { value.SerializedValue = setting.Value.ValueXml.InnerText; // Mark the value as not deserialized, which will trigger a deserialization of the SerializedValue into the PropertyValue. value.Deserialized = false; } }
public static bool ChequearWS() { //return ChequearUrl("https://servicios1.afip.gov.ar/wsfe/service.asmx"); //return ChequearUrl(Properties.Settings.Default.MFD_ar_gov_afip_wswhomo_Service); Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSectionGroup configGroup = config.SectionGroups["applicationSettings"]; ClientSettingsSection settingsSection = (ClientSettingsSection)configGroup.Sections["MFDService.Properties.Settings"]; SettingElementCollection elements = settingsSection.Settings; SettingElement ele = settingsSection.Settings.Get("MFDService_ar_gov_afip_wswhomo_Service"); string url = ele.Value.ValueXml.InnerXml; return(ChequearUrl(url)); }
/// <summary> /// Updates the <see cref="SettingElementCollection"/> from the <see cref="SettingsPropertyValueCollection"/>. /// </summary> /// <param name="settings">A collection representing the settings.</param> /// <param name="values"> /// A <see cref="T:System.Configuration.SettingsPropertyValueCollection"/> representing the /// group of property settings to set. /// </param> private static void UpdateSettingsFromPropertyValues(SettingElementCollection settings, SettingsPropertyValueCollection values) { foreach (SettingsPropertyValue value in values) { if (value.IsDirty) { var element = settings.Get(value.Name); if (element == null) { // Note: We only support string serialization for brevity of implementation. element = new SettingElement(value.Name, SettingsSerializeAs.String); settings.Add(element); } element.SerializeAs = SettingsSerializeAs.String; element.Value.ValueXml = CreateXmlValue(value.SerializedValue); } } }
public static bool AddConfigSettingsCustomSectionSetting(string sectionName, string keyName, string value) { bool bMethodReturnValue = false; try { ConfigurationManager.RefreshSection(sectionName); Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ClientSettingsSection confSect = (ClientSettingsSection)configFile.GetSection(sectionName); if (confSect == null) { ClientSettingsSection adding = new ClientSettingsSection(); configFile.Sections.Add(sectionName, adding); configFile.Save(ConfigurationSaveMode.Modified, true); confSect = (ClientSettingsSection)configFile.GetSection(sectionName); } SettingElementCollection appSettings = confSect.Settings; if (appSettings.Get(keyName) == null) { SettingElement se = new SettingElement(keyName, SettingsSerializeAs.String); SettingValueElement sve = new SettingValueElement { ValueXml = new System.Xml.XmlDocument { InnerXml = $@"<value>{value}</value>" } }; se.Value = sve; appSettings.Add(se); bMethodReturnValue = true; } configFile.Save(ConfigurationSaveMode.Modified, true); ConfigurationManager.RefreshSection(confSect.SectionInformation.Name); } catch (Exception excpt) { Trace.WriteLine($@"{excpt.Message} {excpt.Source} {excpt.StackTrace}"); } return(bMethodReturnValue); }
// SEE ABOVE REFERENCE ** /* * <?XML version ="1.0' encoding="utf-8"> * <configuration> * <configSections> * ... * <section name="{name of section}" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> * ... * </configSections> * <applicationSettings> * <setting name="settingName" serializeAs="String"> * <value>settingValue</value> * <setting name="Frankeinstein" serializeAs="String"> * <value>The Monster is ALIVE!</value> * </setting> * </setting> * </applicationSettings> * <{name of section}> * <setting name="{setting name}" serializeAs="String"> * <value>{setting value}</value> * </setting> * </{name of section}> * </{name of section}> */ public static string ReturnCustomSectionConfigSettingsSettingKeyValue(string sectionName, string keyName) { string appSettingsString = ""; try { ConfigurationManager.RefreshSection(sectionName); Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ClientSettingsSection confSect = (ClientSettingsSection)configFile.GetSection(sectionName); SettingElementCollection appSettings = confSect.Settings; var v = appSettings.Get(keyName); appSettingsString = Convert.ToString(v.Value.ValueXml.InnerText); } catch (Exception excpt) { Trace.WriteLine($@"{excpt.Message} {excpt.Source} {excpt.StackTrace}"); } return(appSettingsString); }
public static string UrlWSConfig(string key) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSectionGroup configGroup = config.SectionGroups["applicationSettings"]; if (configGroup != null) { ClientSettingsSection settingsSection = (ClientSettingsSection)configGroup.Sections["MFDService.Properties.Settings"]; if (settingsSection != null) { SettingElementCollection elements = settingsSection.Settings; if (elements != null) { SettingElement ele = settingsSection.Settings.Get(key); if (ele != null) { return(ele.Value.ValueXml.InnerXml); } } } } return(ReadDllConfig(key)); }
/// <summary> /// Merges the specified user and solution settings into a new <see cref="SettingsPropertyValueCollection"/>. /// </summary> /// <param name="userSettings">The user settings.</param> /// <param name="solutionSettings">The solution settings.</param> /// <param name="properties">The setting properties collection.</param> /// <returns>A merged <see cref="SettingsPropertyValueCollection"/>.</returns> private static SettingsPropertyValueCollection MergeSettingsIntoPropertyValues(SettingElementCollection userSettings, SettingElementCollection solutionSettings, SettingsPropertyCollection properties) { var values = new SettingsPropertyValueCollection(); foreach (SettingsProperty property in properties) { var value = new SettingsPropertyValue(property); ApplySettingToValue(value, userSettings); ApplySettingToValue(value, solutionSettings); value.IsDirty = false; values.Add(value); } return(values); }
/// <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 CollectionGetNonExistent() { SettingElementCollection c = new SettingElementCollection(); Assert.IsNull(c.Get("nonexistent")); }
/// ------------------------------------------------------------------------------------ /// <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 SettingElementCollectionWrapper(SettingElementCollection settings) { _settings = settings; }