简体   繁体   English

Settings.Default.Upgrade() 不保留当前设置

[英]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\ 中的文件夹结构如下所示:

  • myApp.exe_Url_5s2axp5sywfyhblm3201qetpqnmwnvsc myApp.exe_Url_5s2axp5sywfyhblm3201qetpqnmwnvsc
  • myApp.exe_Url_ft4ih1ze0qsz5abu11t334omxo1431c0 myApp.exe_Url_ft4ih1ze0qsz5abu11t334omxo1431c0
  • myApp.exe_Url_glsc2d3cjmswry2bxebb53jndfptav1x myApp.exe_Url_glsc2d3cjmswry2bxebb53jndfptav1x
  • myApp.exe_Url_qngn1rqmbfyy42fdgpmc3ystsaknuxnv myApp.exe_Url_qngn1rqmbfyy42fdgpmc3ystsaknuxnv
  • myApp.exe_Url_vqn0ogftrchl1fild5fe34hmijvmd2zr myApp.exe_Url_vqn0ogftrchl1fild5fe34hmijvmd2zr

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:我不知道所描述行为的原因,但以下两件事之一修复了它:

  • Added the default manifest file and changed the "assemblyIdentity" node (the "version" attribute does not seem to have an effect on the Settings)添加了默认清单文件并更改了“assemblyIdentity”节点(“version”属性似乎对设置没有影响)
  • Activated the signing feature of Visual Studio (Project properties --> Signing)激活 Visual Studio 的签名功能(项目属性 --> 签名)

项目 -> 属性 -> 应用程序 -> 清单 = 使用默认设置嵌入清单

@cleo Thanks for solution. @cleo 感谢您的解决方案。 Signing fixed the issue.签名解决了这个问题。 The folder name no longer changes.文件夹名称不再更改。

\AppData\Local\MyAppName.exe_ StrongName _iyq1qat10dlrezghdzsmthpr49hlodkj \AppData\Local\MyAppName.exe_ StrongName _iyq1qat10dlrezghdzsmthpr49hlodkj

在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM