コード例 #1
0
        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);
            }
        }
コード例 #2
0
        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");
        }
コード例 #3
0
        /// <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;
            }
        }
コード例 #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);
                }
            }
        }
コード例 #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);
        }
コード例 #6
0
        // 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);
        }
コード例 #7
0
        public void CollectionGetNonExistent()
        {
            SettingElementCollection c = new SettingElementCollection();

            Assert.IsNull(c.Get("nonexistent"));
        }
コード例 #8
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();
            }
        }
コード例 #9
0
 public string this[string key]
 {
     get { return(WPFHelpers.DesignMode ? "" : _settings.Get(key).Value.ValueXml.InnerText); }
     set { _settings.Get(key).Value.ValueXml.InnerText = value; }
 }