public void GetUserScopedPropertyValues()
        {
            SettingsAttributeDictionary dict = new SettingsAttributeDictionary();
            UserScopedSettingAttribute  attr = new UserScopedSettingAttribute();

            dict.Add(attr.GetType(), attr);

            LocalFileSettingsProvider prov = new LocalFileSettingsProvider();
            SettingsContext           ctx  = new SettingsContext();
            SettingsProperty          p    = new SettingsProperty("property",
                                                                  typeof(int),
                                                                  prov,
                                                                  false,
                                                                  10,
                                                                  SettingsSerializeAs.Binary,
                                                                  dict,
                                                                  false,
                                                                  false);
            SettingsPropertyCollection      col = new SettingsPropertyCollection();
            SettingsPropertyValueCollection vals;

            col.Add(p);

            prov.Initialize(null, null);

            vals = prov.GetPropertyValues(ctx, col);
            Assert.IsNotNull(vals, "A1");
            Assert.AreEqual(1, vals.Count, "A2");
        }
Пример #2
0
        /// <summary>
        /// Updates application settings to reflect a more recent installation of the application.
        /// </summary>
        public override void Upgrade()
        {
            var oldSettingsProvider = new LocalFileSettingsProvider();
            var oldPropertyValues   = oldSettingsProvider.GetPropertyValues(Context, Properties);

            foreach (SettingsPropertyValue oldPropertyValue in oldPropertyValues)
            {
                if (!Equals(this[oldPropertyValue.Name], oldPropertyValue.PropertyValue))
                {
                    this[oldPropertyValue.Name] = oldPropertyValue.PropertyValue;
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Applies user settings values to settings properties.
        /// </summary>
        /// <param name="context">A <c>System.Configuration.SettingsContext</c> that describes where the application settings property is used.</param>
        /// <param name="userConfiguration">User configuration to retrieve settings from.</param>
        /// <param name="appConfiguration">Fallback application configuration to retrieve settings from once User configuration file not found.</param>
        /// <param name="settingPropertyValues">Collection of <c>Settings</c> associated with thier values.</param>
        /// <param name="userSettingsProperties"><c>Settings</c> user scoped properties, that needs to be initialized.</param>
        void ApplyUserSettingsValuesToConfigurationElements(
            SettingsContext context,
            Configuration userConfiguration,
            Configuration appConfiguration,
            SettingsPropertyValueCollection settingPropertyValues,
            IEnumerable <SettingsProperty> userSettingsProperties)
        {
            string settingsGroupName = Convert.ToString(context[SettingsGroupNameContextKey]);

            var initializedSettings = new List <SettingsProperty>();

            //Once corresponding 'user.config' file exists - use default settings provider to initialize user settings:
            if (userConfiguration.HasFile)
            {
                var settingsActuallySet = XDocument.
                                          Load(userConfiguration.FilePath).
                                          XPathSelectElements(string.Format("/configuration/{0}/{1}/setting", UserSettingsGroupName, settingsGroupName)).
                                          Select(settingElement => settingElement.Attribute("name").Value).
                                          ToList();

                var userConfigDeclaredSettings = userSettingsProperties.
                                                 Where(setting => settingsActuallySet.Contains(setting.Name)).
                                                 ToList();

                var userPropertySettingsCollection = new SettingsPropertyCollection();
                userConfigDeclaredSettings.ForEach(userPropertySettingsCollection.Add);

                var defaultProvider = new LocalFileSettingsProvider();
                defaultProvider.Initialize(ApplicationName, _settingValues);
                var userPropertyValues = defaultProvider.GetPropertyValues(context, userPropertySettingsCollection);

                userPropertyValues.Cast <SettingsPropertyValue>().
                ToList().
                ForEach(settingPropertyValues.Add);

                initializedSettings.AddRange(userConfigDeclaredSettings);
            }
            //Reading defaults from application configuration:
            appConfiguration.
            With(configuration => configuration.SectionGroups[UserSettingsGroupName]).
            With(sectionGroup => sectionGroup.Sections[settingsGroupName].As <ClientSettingsSection>()).
            With(section => section.Settings.Cast <SettingElement>()).
            Do(userSettings =>
               ApplySettingsValuesToConfigurationElements(
                   settingPropertyValues,
                   userSettings,
                   userSettingsProperties.Except(initializedSettings),
                   (prop, configurationElement) => prop.Name == configurationElement.Name));
        }
        public void GetPropertyValues_NotStoredProperty_ValueEqualsNull()
        {
            var property = new SettingsProperty("PropertyName");

            property.Attributes.Add(typeof(UserScopedSettingAttribute), new UserScopedSettingAttribute());
            var properties = new SettingsPropertyCollection();

            properties.Add(property);
            var localFileSettingsProvider = new LocalFileSettingsProvider();

            SettingsPropertyValueCollection propertyValues = localFileSettingsProvider.GetPropertyValues(_testContext, properties);

            Assert.Equal(1, propertyValues.Count);
            Assert.Null(propertyValues["PropertyName"].PropertyValue);
        }
Пример #5
0
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
        {
            try
            {
                if (_localFileSettingProvider != null)
                {
                    return(_localFileSettingProvider.GetPropertyValues(context, collection));
                }
            }
            catch (ConfigurationErrorsException)
            {
                // ignore the error, fall back to the default values
            }

            return(GetDefaultValues(context, collection));
        }
        public void GetPropertyValues_NotStoredConnectionStringProperty_ValueEqualsEmptyString()
        {
            var property = new SettingsProperty("PropertyName");

            property.PropertyType = typeof(string);
            property.Attributes.Add(typeof(ApplicationScopedSettingAttribute), new ApplicationScopedSettingAttribute());
            property.Attributes.Add(typeof(SpecialSettingAttribute), new SpecialSettingAttribute(SpecialSetting.ConnectionString));
            var properties = new SettingsPropertyCollection();

            properties.Add(property);
            var localFileSettingsProvider = new LocalFileSettingsProvider();

            SettingsPropertyValueCollection propertyValues = localFileSettingsProvider.GetPropertyValues(_testContext, properties);

            Assert.Equal(1, propertyValues.Count);
            Assert.Equal(string.Empty, propertyValues["PropertyName"].PropertyValue);
        }
 public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
 {
     return(_provider.GetPropertyValues(context, collection));
 }