[英]Settings.Default.Upgrade() does not keep current settings
I have a WPF application that uses the built-in Settings functionality.我有一个使用内置设置功能的 WPF 应用程序。 When I release a new version of the application, I increase the assembly version and execute the following code at application start:当我发布新版本的应用程序时,我会增加程序集版本并在应用程序启动时执行以下代码:
if (Settings.Default.IsSettingsUpgradeRequired) //this defaults to true when a new version of this software has been released
{
Settings.Default.Upgrade(); //upgrade the settings to the newer version
Settings.Default.Reload();
Settings.Default.IsSettingsUpgradeRequired = false;
Settings.Default.LastSettingsUpdate = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
Settings.Default.Save();
}
The problem is that the previous settings are not maintained.问题是没有保留以前的设置。 Instead, a new folder under \AppData\Local\ is created each time a new version comes up.相反,每次出现新版本时都会在 \AppData\Local\ 下创建一个新文件夹。 Thus, the default settings are used instead of those of the previous version.因此,使用默认设置而不是以前版本的设置。 I know that, under normal circumstances, there should be ONE folder that contains many sub-folders with the application's version as name.我知道,在正常情况下,应该有一个文件夹,其中包含许多以应用程序版本为名称的子文件夹。 Instead, I have MANY folders each containing only one folder with the application's version as name.相反,我有许多文件夹,每个文件夹只包含一个以应用程序版本作为名称的文件夹。 The folder structure in Local\ looks like this: Local\ 中的文件夹结构如下所示:
So how do I stop the system to create so many folders and make it only use one folder per application so that I can properly upgrade my settings?那么,如何阻止系统创建这么多文件夹并使其每个应用程序只使用一个文件夹,以便我可以正确升级我的设置?
Edit: another thing I noticed today is that if I change the location of the application's folder (lets say move it from desktop to C:\myApp), the application creates a new settings folder when first started.编辑:我今天注意到的另一件事是,如果我更改应用程序文件夹的位置(假设将其从桌面移动到 C:\myApp),应用程序会在首次启动时创建一个新的设置文件夹。 Why does the system not recognize it as the same application?为什么系统不能将其识别为相同的应用程序?
I finally got it to work.我终于让它工作了。 I don't know the cause for the desrcibed behavior, but one of these 2 things fixed it:我不知道所描述行为的原因,但以下两件事之一修复了它:
项目 -> 属性 -> 应用程序 -> 清单 = 使用默认设置嵌入清单
I'm using .NET6 in 2022 and just adding this during app start worked for me:我在 2022 年使用 .NET6,只是在应用程序启动期间添加它对我有用:
Settings.Default.Upgrade();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.