internal static void MigrateUserSettings(ApplicationSettingsBase settings) { if (settings is IMigrateSettings) { IMigrateSettings customMigrator = (IMigrateSettings)settings; foreach (SettingsProperty property in settings.Properties) { if (!SettingsPropertyExtensions.IsUserScoped(property)) { continue; } object previousValue = settings.GetPreviousVersion(property.Name); //need to do this to force the values to load before accessing the PropertyValues in order to migrate, //otherwise the SettingsPropertyValue will always be null. var iForceSettingsPropertyValuesToLoad = settings[property.Name]; var currentValue = settings.PropertyValues[property.Name]; MigrateProperty(customMigrator, MigrationScope.User, currentValue, previousValue); } } else { settings.Upgrade(); } //Don't need to reload because the user settings will be current. SaveIfDirty(settings); }
private static void MigrateProperty(IMigrateSettings customMigrator, MigrationScope migrationScope, SettingsPropertyValue currentValue, object previousValue) { var migrationValues = new SettingsPropertyMigrationValues( currentValue.Property.Name, migrationScope, currentValue.PropertyValue, previousValue); customMigrator.MigrateSettingsProperty(migrationValues); if (!Equals(migrationValues.CurrentValue, currentValue.PropertyValue)) currentValue.PropertyValue = migrationValues.CurrentValue; }
private static void MigrateProperty(IMigrateSettings customMigrator, MigrationScope migrationScope, SettingsPropertyValue currentValue, object previousValue) { var migrationValues = new SettingsPropertyMigrationValues( currentValue.Property.Name, migrationScope, currentValue.PropertyValue, previousValue); customMigrator.MigrateSettingsProperty(migrationValues); if (!Equals(migrationValues.CurrentValue, currentValue.PropertyValue)) { currentValue.PropertyValue = migrationValues.CurrentValue; } }
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(); }