public static void SetSharedPropertyValues(LocalFileSettingsProvider provider, SettingsContext context, SettingsPropertyValueCollection values, string currentExeConfigFilename)
        {
            var valuesToStore = new Dictionary <string, string>();

            foreach (SettingsPropertyValue value in values)
            {
                if (value.SerializedValue != null)
                {
                    valuesToStore[value.Name] = (string)value.SerializedValue;
                }
            }

            var systemConfiguration = String.IsNullOrEmpty(currentExeConfigFilename)
                                          ? SystemConfigurationHelper.GetExeConfiguration()
                                          : SystemConfigurationHelper.GetExeConfiguration(currentExeConfigFilename);

            var settingsClass = (Type)context["SettingsClassType"];

            systemConfiguration.PutSettingsValues(settingsClass, valuesToStore);

            foreach (SettingsPropertyValue value in values)
            {
                value.IsDirty = false;
            }
        }
        public static SettingsPropertyValueCollection GetSharedPropertyValues(LocalFileSettingsProvider provider, SettingsContext context, SettingsPropertyCollection properties, string currentExeConfigFilename = null)
        {
            var settingsClass = (Type)context["SettingsClassType"];

            var systemConfiguration = String.IsNullOrEmpty(currentExeConfigFilename)
                              ? SystemConfigurationHelper.GetExeConfiguration()
                              : SystemConfigurationHelper.GetExeConfiguration(currentExeConfigFilename);

            var storedValues = systemConfiguration.GetSettingsValues(settingsClass);

            // Create new collection of values
            var values = new SettingsPropertyValueCollection();

            foreach (SettingsProperty setting in properties)
            {
                var value = new SettingsPropertyValue(setting)
                {
                    SerializedValue = storedValues.ContainsKey(setting.Name) ? storedValues[setting.Name] : null,
                    IsDirty         = false
                };

                // use the stored value, or set the SerializedValue to null, which tells .NET to use the default value
                values.Add(value);
            }

            return(values);
        }
        public static void RemovePreviousConfigFile(LocalFileSettingsProvider lfsp)
        {
            if (lfsp == null)
            {
                return;
            }

            Type t = lfsp.GetType();

            object o = t.InvokeMember("GetPreviousConfigFileName",
                                      BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase,
                                      null,
                                      lfsp,
                                      new object[] { true }
                                      );

            string configFile = o as string;

            if (!String.IsNullOrEmpty(configFile) && File.Exists(configFile))
            {
                Directory.Delete(Path.GetDirectoryName(configFile), true);
            }

            o = t.InvokeMember("GetPreviousConfigFileName",
                               BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase,
                               null,
                               lfsp,
                               new object[] { false }
                               );
            configFile = o as string;
            if (!String.IsNullOrEmpty(configFile) && File.Exists(configFile))
            {
                Directory.Delete(Path.GetDirectoryName(configFile), true);
            }
        }
        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");
        }
Exemplo n.º 5
0
 public UserSettings()
     : base("settings")
 {
     // provider
     _defaultProvider = new LocalFileSettingsProvider();
     _defaultProvider.Initialize("LocalFileSettingsProvider", null);
     base.Providers.Add(_defaultProvider);
 }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            try
            {
                _localFileSettingProvider = new LocalFileSettingsProvider();
            }
            catch (Exception)
            {
            }

            base.Initialize(ApplicationName, config);
        }
Exemplo n.º 8
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;
                }
            }
        }
        public void Properties()
        {
            LocalFileSettingsProvider prov = new LocalFileSettingsProvider();

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

            prov.ApplicationName = "foo";
            Assert.AreEqual("foo", prov.ApplicationName, "A3");

            prov.ApplicationName = null;
            Assert.IsNull(prov.ApplicationName, "A4");
        }
Exemplo n.º 10
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);
        }
        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);
        }
Exemplo n.º 13
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 static void UpgradeSharedPropertyValues(LocalFileSettingsProvider provider, SettingsContext context, SettingsPropertyCollection properties, string previousExeConfigFilename, string currentExeConfigFilename = null)
        {
            SettingsPropertyValueCollection currentValues  = GetSharedPropertyValues(provider, context, properties, currentExeConfigFilename);
            SettingsPropertyValueCollection previousValues = GetPreviousSharedPropertyValues(provider, context, properties, previousExeConfigFilename);

            foreach (SettingsProperty property in properties)
            {
                SettingsPropertyValue previousValue = previousValues[property.Name];
                if (previousValue != null)
                {
                    SettingsPropertyValue currentValue = currentValues[property.Name];
                    if (currentValue == null)
                    {
                        currentValues.Add(previousValue);
                    }
                    else
                    {
                        currentValue.SerializedValue = previousValue.SerializedValue;
                    }
                }
            }

            SetSharedPropertyValues(provider, context, currentValues, currentExeConfigFilename);
        }
 public ExtendedLocalFileSettingsProvider(LocalFileSettingsProvider provider)
 {
     _provider = provider;
     _appName  = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
 }
        public static SettingsPropertyValueCollection GetPreviousSharedPropertyValues(LocalFileSettingsProvider provider, SettingsContext context, SettingsPropertyCollection properties, string previousExeConfigFilename)
        {
            var values = new SettingsPropertyValueCollection();

            if (String.IsNullOrEmpty(previousExeConfigFilename))
            {
                return(values);
            }

            var settingsClass  = (Type)context["SettingsClassType"];
            var previousValues = new ConfigurationFileReader(previousExeConfigFilename).GetSettingsValues(settingsClass);

            if (previousValues == null)
            {
                return(values);
            }

            foreach (var value in previousValues)
            {
                var property = properties[value.Key];
                if (property == null)
                {
                    continue;
                }

                var settingsValue = new SettingsPropertyValue(property)
                {
                    SerializedValue = value.Value, IsDirty = false
                };
                values.Add(settingsValue);
            }

            return(values);
        }
 public ExtendedLocalFileSettingsProvider(LocalFileSettingsProvider provider)
 {
     _provider = provider;
 }