public static object GetPreviousSharedPropertyValue(this ApplicationSettingsBase settings, string propertyName, string previousExeConfigFilename)
        {
            SettingsProperty property = settings.Properties[propertyName];

            if (property == null)
            {
                throw new ArgumentException(String.Format("The specified property does not exist: {0}", propertyName), "propertyName");
            }

            ISharedApplicationSettingsProvider provider = GetSharedSettingsProvider(property.Provider);

            if (provider == null)
            {
                return(null);
            }

            SettingsPropertyValueCollection values = provider.GetPreviousSharedPropertyValues(settings.Context,
                                                                                              new SettingsPropertyCollection {
                property
            }, previousExeConfigFilename);

            SettingsPropertyValue value = values[propertyName];

            return(value == null ? null : value.PropertyValue);
        }
        private static void SetSharedPropertyValues(ApplicationSettingsBase settings, Dictionary <string, string> values)
        {
            foreach (SettingsProvider provider in settings.Providers)
            {
                ISharedApplicationSettingsProvider sharedSettingsProvider = GetSharedSettingsProvider(provider);
                if (sharedSettingsProvider == null)
                {
                    throw new NotSupportedException("Setting shared values is not supported.");
                }

                var properties = GetPropertiesForProvider(settings, provider);
                SettingsPropertyValueCollection settingsValues = new SettingsPropertyValueCollection();

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

                    settingsValues.Add(new SettingsPropertyValue(property)
                    {
                        SerializedValue = value.Value, IsDirty = true
                    });
                }

                sharedSettingsProvider.SetSharedPropertyValues(settings.Context, settingsValues);
            }

            SaveIfDirty(settings);
            settings.Reload();
        }
        public static void SetSharedPropertyValue(this ApplicationSettingsBase settings, string propertyName, object value)
        {
            SettingsProperty property = settings.Properties[propertyName];

            if (property == null)
            {
                throw new ArgumentException(String.Format("The specified property does not exist: {0}", propertyName), "propertyName");
            }

            ISharedApplicationSettingsProvider sharedSettingsProvider = GetSharedSettingsProvider(property.Provider);

            if (sharedSettingsProvider == null)
            {
                throw new NotSupportedException("Setting shared values is not supported.");
            }

            SettingsPropertyValue settingsValue = new SettingsPropertyValue(property)
            {
                PropertyValue = value
            };

            sharedSettingsProvider.SetSharedPropertyValues(settings.Context, new SettingsPropertyValueCollection {
                settingsValue
            });

            SaveIfDirty(settings);
            //Need to call Reload because changes to shared settings aren't automatically realized by the .NET settings framework.
            settings.Reload();
        }
        internal static void MigrateSharedSettings(ApplicationSettingsBase settings, string previousExeConfigFilename)
        {
            if (settings is IMigrateSettings)
            {
                IMigrateSettings customMigrator = (IMigrateSettings)settings;
                foreach (SettingsProvider settingsProvider in settings.Providers)
                {
                    ISharedApplicationSettingsProvider sharedSettingsProvider = GetSharedSettingsProvider(settingsProvider);
                    if (sharedSettingsProvider == null || !sharedSettingsProvider.CanUpgradeSharedPropertyValues(settings.Context))
                    {
                        continue;
                    }

                    var properties     = GetPropertiesForProvider(settings, settingsProvider);
                    var previousValues = sharedSettingsProvider.GetPreviousSharedPropertyValues(settings.Context,
                                                                                                properties, previousExeConfigFilename);
                    if (previousValues == null || previousValues.Count == 0)
                    {
                        continue;
                    }

                    var currentValues = sharedSettingsProvider.GetSharedPropertyValues(settings.Context, properties);
                    foreach (SettingsPropertyValue previousValue in previousValues)
                    {
                        SettingsPropertyValue currentValue = currentValues[previousValue.Name];
                        if (currentValue == null)
                        {
                            continue;
                        }

                        MigrateProperty(customMigrator, MigrationScope.Shared, currentValue, previousValue.PropertyValue);
                    }

                    foreach (SettingsPropertyValue property in currentValues)
                    {
                        if (!property.IsDirty)
                        {
                            continue;
                        }

                        sharedSettingsProvider.SetSharedPropertyValues(settings.Context, currentValues);
                        break;
                    }
                }
            }
            else
            {
                foreach (SettingsProvider settingsProvider in settings.Providers)
                {
                    ISharedApplicationSettingsProvider sharedSettingsProvider = GetSharedSettingsProvider(settingsProvider);
                    if (sharedSettingsProvider == null)
                    {
                        continue;
                    }

                    var properties = GetPropertiesForProvider(settings, settingsProvider);
                    sharedSettingsProvider.UpgradeSharedPropertyValues(settings.Context, properties, previousExeConfigFilename);
                }
            }

            SaveIfDirty(settings);
            //Need to call Reload because changes to shared settings aren't automatically realized by the .NET settings framework.
            settings.Reload();
        }