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); }
public static void EnsureMaintainUserSettings(this ApplicationSettingsBase settings, string keyName) { if (settings == null) { return; } var keyValue = settings[keyName]; var defaultValue = settings.Properties[keyName]?.DefaultValue; if (keyValue != defaultValue) { return; } foreach (SettingsProperty prop in settings.Properties) { if (!prop.Attributes.ContainsKey(typeof(UserScopedSettingAttribute))) { continue; } var result = settings.GetPreviousVersion(prop.Name); settings[prop.Name] = result; } settings.Save(); }