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>
        /// Sets the values of the specified group of property settings.
        /// </summary>
        /// <param name="context">A <c>System.Configuration.SettingsContext</c> that describes where the application settings property is used.</param>
        /// <param name="values">A <c>System.Configuration.SettingsPropertyValueCollection</c> representing the group of property settings to set.</param>
        /// <exception cref="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>
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection values)
        {
            //Only user scope settings could be saved, so delegating this process to standard LocalFileSettingsProvider:
            var defaultProvider = new LocalFileSettingsProvider();

            defaultProvider.Initialize(ApplicationName, _settingValues);
            defaultProvider.SetPropertyValues(context, values);
        }
Пример #3
0
 public UserSettings()
     : base("settings")
 {
     // provider
     _defaultProvider = new LocalFileSettingsProvider();
     _defaultProvider.Initialize("LocalFileSettingsProvider", null);
     base.Providers.Add(_defaultProvider);
 }
Пример #4
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 override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            if (string.IsNullOrEmpty(name))
            {
                name = ApplicationName;
            }

            if (_ownProvider)
            {
                //Otherwise it's already been initialized and we're just temporarily wrapping it.
                _provider.Initialize(null, null);
            }

            base.Initialize(name, config);
        }
Пример #6
0
        }         // ApplicationSettings

        // ----------------------------------------------------------------------
        public ApplicationSettings(string settingsKey, object obj) :
            base(settingsKey)
        {
            settings = new SettingCollection(this);

            // provider
            defaultProvider = new LocalFileSettingsProvider();
            defaultProvider.Initialize("LocalFileSettingsProvider", new NameValueCollection());
            base.Providers.Add(defaultProvider);

            // upgrade
            upgradeSettings = new ValueSetting(UpgradeSettingsKey, typeof(bool), true);
            UseAutoUpgrade  = true;

            if (obj != null)
            {
                Settings.AddAll(obj);
            }
        }         // ApplicationSettings
        public void Initialized()
        {
            LocalFileSettingsProvider prov = new LocalFileSettingsProvider();

            prov.Initialize(null, null);

            // defaults, uninitialized
            Assert.AreEqual("LocalFileSettingsProvider", prov.Name, "A1");
            Assert.AreEqual("", prov.ApplicationName, "A2");

            prov = new LocalFileSettingsProvider();
            NameValueCollection nv = new NameValueCollection();

            nv.Add("applicationName", "appName");

            prov.Initialize("hi", nv);
            // As these lines below shows, Initialize() behavior is unpredictable. Here I just comment out them and fix run-test-ondotnet tests.
            //Assert.AreEqual ("hi", prov.Name, "A3");
            //Assert.AreEqual ("hi", prov.Description, "A3.5");
            //Assert.AreEqual ("", prov.ApplicationName, "A4");
        }
 public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
 {
     _provider.Initialize(name, config);
 }