我已经明白我可以使用以下代码保留以前版本的用户设置:
if (Settings.Default.UpgradeRequired)
{
Settings.Default.Upgrade();
Settings.Default.UpgradeRequired = false;
Settings.Default.Save();
}
但是,如果我更改设置的漫游属性,那似乎不起作用。当我将设置从漫游更改为本地或反之时,有没有办法让设置值继续进行而不能重置?
编辑:我研究了使用GetPreviousVersion()
方法将漫游设置升级到本地设置的可能方法,但它不起作用,因为如果当前设置不是先前版本的设置正在漫游,之前的版本根本没有退回。
重现:
true
。User
。运行以下代码:
Console.WriteLine(Settings.Default.GetPreviousVersion("MySetting"));
Settings.Default.MySetting = "Not the default value.";
Settings.Default.Save();
false
。答案 0 :(得分:1)
如果您知道哪些属性已从roaming = true更改为roaming = false,那么您可以手动将SettingsManageabilityAttribute
添加到SettingsProperty.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"));