2

我开始明白我可以使用以下代码保留以前版本的用户设置:

        if (Settings.Default.UpgradeRequired)
        {
            Settings.Default.Upgrade();
            Settings.Default.UpgradeRequired = false;
            Settings.Default.Save();
        }

但是,如果我更改设置的漫游属性,这似乎不起作用。当我将设置从漫游更改为本地或反之亦然时,有什么方法可以让设置值结转而不重置?

编辑:我研究了一种使用该方法将漫游设置升级到本地设置的可能GetPreviousVersion()方法,但它不起作用,因为如果设置的先前版本在当前设置不是漫游时,则不会返回先前版本一点也不。

重现:

  1. 进行名为 MySetting 的设置。
  2. 将 MySetting 的 Roaming 属性更改为true
  3. 确保 MySetting 的范围是User.
  4. 运行以下代码:

        Console.WriteLine(Settings.Default.GetPreviousVersion("MySetting"));
        Settings.Default.MySetting = "Not the default value.";
        Settings.Default.Save();
    
  5. 增加程序集版本。
  6. 再次运行代码,注意输出了新值。
  7. 将 MySetting 的 Roaming 属性更改为false
  8. 再次增加程序集版本。
  9. 再次运行代码,注意输出的是默认值。
4

1 回答 1

1

如果您知道哪些属性已从 roaming=true 更改为 roaming=false,那么您可以手动将 添加SettingsManageabilityAttributeSettingsProperty.Attributes字典中,用于GetPreviousVersion检索以前的值,然后从字典中删除该属性以进行清理:

Console.WriteLine("Current: {0}", Settings.Default.MySetting);
// we don't see the previous value here...
Console.WriteLine("Previous: {0}", Settings.Default.GetPreviousVersion("MySetting"));
// ...so we manually add the SettingsManageabilityAttribute to it...
var setting = Settings.Default.Properties["MySetting"];
setting.Attributes.Add(typeof(SettingsManageabilityAttribute), new SettingsManageabilityAttribute(SettingsManageability.Roaming));
// ...retrieve the previous value...
Console.WriteLine("Previous: {0}", Settings.Default.GetPreviousVersion("MySetting"));
// ...and then clean up after ourselves by removing the attribute.
setting.Attributes.Remove(typeof(SettingsManageabilityAttribute));
// ...now we don't see the previous value anymore.
Console.WriteLine("Previous: {0}", Settings.Default.GetPreviousVersion("MySetting"));
于 2017-08-04T19:32:00.500 回答