Ejemplo n.º 1
0
        /// <summary>
        /// Retrieves the values of settings from the given config file (as opposed to using
        /// the configuration for the current context)
        /// </summary>
        private SettingsPropertyValueCollection GetSettingValuesFromFile(string configFileName, string sectionName, bool userScoped, SettingsPropertyCollection properties)
        {
            SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();
            IDictionary settings = ClientSettingsStore.ReadSettingsFromFile(configFileName, sectionName, userScoped);

            // Map each SettingProperty to the right StoredSetting and deserialize the value if found.
            foreach (SettingsProperty setting in properties)
            {
                string settingName          = setting.Name;
                SettingsPropertyValue value = new SettingsPropertyValue(setting);

                if (settings.Contains(settingName))
                {
                    StoredSetting ss          = (StoredSetting)settings[settingName];
                    string        valueString = ss.Value.InnerXml;

                    // We need to un-escape string serialized values
                    if (ss.SerializeAs == SettingsSerializeAs.String)
                    {
                        valueString = Escaper.Unescape(valueString);
                    }

                    value.SerializedValue = valueString;
                    value.IsDirty         = true;
                    values.Add(value);
                }
            }

            return(values);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Implementation of IClientSettingsProvider.Reset. Resets user scoped settings to the values
        ///     in app.exe.config, does nothing for app scoped settings.
        /// </summary>
        public void Reset(SettingsContext context)
        {
            string sectionName = GetSectionName(context);

            // First revert roaming, then local
            ClientSettingsStore.RevertToParent(sectionName, true);
            ClientSettingsStore.RevertToParent(sectionName, false);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Abstract SettingsProvider method
        /// </summary>
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection values)
        {
            string      sectionName         = GetSectionName(context);
            IDictionary roamingUserSettings = new Hashtable();
            IDictionary localUserSettings   = new Hashtable();

            foreach (SettingsPropertyValue value in values)
            {
                SettingsProperty setting       = value.Property;
                bool             isUserSetting = IsUserSetting(setting);

                if (value.IsDirty)
                {
                    if (isUserSetting)
                    {
                        bool          isRoaming = IsRoamingSetting(setting);
                        StoredSetting ss        = new StoredSetting(setting.SerializeAs, SerializeToXmlElement(setting, value));

                        if (isRoaming)
                        {
                            roamingUserSettings[setting.Name] = ss;
                        }
                        else
                        {
                            localUserSettings[setting.Name] = ss;
                        }

                        value.IsDirty = false; //reset IsDirty
                    }
                    else
                    {
                        // This is an app-scoped or connection string setting that has been written to.
                        // We don't support saving these.
                    }
                }
            }

            // Semi-hack: If there are roamable settings, let's write them before local settings so if a handler
            // declaration is necessary, it goes in the roaming config file in preference to the local config file.
            if (roamingUserSettings.Count > 0)
            {
                ClientSettingsStore.WriteSettings(sectionName, true, roamingUserSettings);
            }

            if (localUserSettings.Count > 0)
            {
                ClientSettingsStore.WriteSettings(sectionName, false, localUserSettings);
            }
        }
Ejemplo n.º 4
0
        private SettingsPropertyValueCollection GetSettingValuesFromFile(string configFileName, string sectionName, bool userScoped, SettingsPropertyCollection properties)
        {
            SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();
            IDictionary dictionary = ClientSettingsStore.ReadSettingsFromFile(configFileName, sectionName, userScoped);

            foreach (SettingsProperty property in properties)
            {
                string name = property.Name;
                SettingsPropertyValue value2 = new SettingsPropertyValue(property);
                if (dictionary.Contains(name))
                {
                    StoredSetting setting  = (StoredSetting)dictionary[name];
                    string        innerXml = setting.Value.InnerXml;
                    if (setting.SerializeAs == SettingsSerializeAs.String)
                    {
                        innerXml = this.Escaper.Unescape(innerXml);
                    }
                    value2.SerializedValue = innerXml;
                    value2.IsDirty         = true;
                    values.Add(value2);
                }
            }
            return(values);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Abstract SettingsProvider method
        /// </summary>
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection properties)
        {
            SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();
            string sectionName = GetSectionName(context);

            // Look for this section in both applicationSettingsGroup and userSettingsGroup
            IDictionary appSettings  = ClientSettingsStore.ReadSettings(sectionName, false);
            IDictionary userSettings = ClientSettingsStore.ReadSettings(sectionName, true);
            ConnectionStringSettingsCollection connStrings = ClientSettingsStore.ReadConnectionStrings();

            // Now map each SettingProperty to the right StoredSetting and deserialize the value if found.
            foreach (SettingsProperty setting in properties)
            {
                string settingName          = setting.Name;
                SettingsPropertyValue value = new SettingsPropertyValue(setting);

                // First look for and handle "special" settings
                SpecialSettingAttribute attr = setting.Attributes[typeof(SpecialSettingAttribute)] as SpecialSettingAttribute;
                bool isConnString            = (attr != null) ? (attr.SpecialSetting == SpecialSetting.ConnectionString) : false;

                if (isConnString)
                {
                    string connStringName = sectionName + "." + settingName;
                    if (connStrings != null && connStrings[connStringName] != null)
                    {
                        value.PropertyValue = connStrings[connStringName].ConnectionString;
                    }
                    else if (setting.DefaultValue != null && setting.DefaultValue is string)
                    {
                        value.PropertyValue = setting.DefaultValue;
                    }
                    else
                    {
                        //No value found and no default specified
                        value.PropertyValue = string.Empty;
                    }

                    value.IsDirty = false; //reset IsDirty so that it is correct when SetPropertyValues is called
                    values.Add(value);
                    continue;
                }

                // Not a "special" setting
                bool isUserSetting = IsUserSetting(setting);

                if (isUserSetting && !ConfigurationManagerInternalFactory.Instance.SupportsUserConfig)
                {
                    // We encountered a user setting, but the current configuration system does not support
                    // user settings.
                    throw new ConfigurationErrorsException(SR.UserSettingsNotSupported);
                }

                IDictionary settings = isUserSetting ? userSettings : appSettings;

                if (settings.Contains(settingName))
                {
                    StoredSetting ss          = (StoredSetting)settings[settingName];
                    string        valueString = ss.Value.InnerXml;

                    // We need to un-escape string serialized values
                    if (ss.SerializeAs == SettingsSerializeAs.String)
                    {
                        valueString = Escaper.Unescape(valueString);
                    }

                    value.SerializedValue = valueString;
                }
                else if (setting.DefaultValue != null)
                {
                    value.SerializedValue = setting.DefaultValue;
                }
                else
                {
                    //No value found and no default specified
                    value.PropertyValue = null;
                }

                value.IsDirty = false; //reset IsDirty so that it is correct when SetPropertyValues is called
                values.Add(value);
            }

            return(values);
        }