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) {
     }
 }
Exemple #4
0
        /// <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);
                }
            }
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        /// ------------------------------------------------------------------------------------
        /// <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();
            }
        }