如何在文件版本更改期间保留 .settings/.config 文件中的更改?

发布于 2024-08-26 00:24:03 字数 392 浏览 5 评论 0原文

我创建了一个使用 settings.settings 来存储一些用户特定设置(scope=User)的应用程序。设置在启动时正确加载,在使用过程中更改并正确保存以供下次启动。这个循环看起来没有问题。

当我更新新版本的程序集和文件版本时,就会出现问题。启动时不再加载设置(而是使用默认值)。即使启动版本 1.2 并且生成并保存了一个新的配置文件,从版本 1.1 保存的配置文件似乎也会保留(即您可以重新启动版本 1.1,配置文件将是从该版本保存的配置文件)版本)。

因此,这些设置似乎特定于程序集和/或文件的版本。还值得注意的是,在版本 1.1 和版本 1.2 之间,settings.settings 文件或其他任何内容都没有更改(即我在这些不同版本之间所做的唯一更改是修改版本号)。

有没有办法在版本更改时保留这些设置?

I have created an application that uses settings.settings to store some user specific settings (scope=User). Settings are loaded correctly on startup, changed during use and saved correctly for next launch. This cycle appears to have no problems.

The problem arises when I update the assembly and file versions for a new build. The settings are no longer loaded on startup (instead the default values are used). It also appears that a config file saved from version 1.1 will persist even if version 1.2 is launched and a NEW config file is generated and saved too (i.e. you can relaunch version 1.1 and the config file will be the config file that was saved from that version).

So it appears that the settings are specific to the version of the assembly and/or file. It is also worth noting that between version 1.1 and version 1.2 there were no changes to the settings.settings file or anything else for that matter (i.e. the only change I made between these different builds was modifying the version numbers).

Is there a way to persist these settings across version changes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巷雨优美回忆 2024-09-02 00:24:03

一些说明:

您必须调用 ApplicationSettingsBase 派生类的 Upgrade 方法(通常称为 Settings,由 Visual Studio 为您创建) ):

Properties.Settings.Default.Upgrade();

何时何地调用 Upgrade 方法?您可以应用一个简单的技巧:将名为 UpgradeRequired(示例)的用户设置定义为 bool(最简单的方法是通过 IDE)。确保其默认值为 true

在应用程序的开头插入这段代码:

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

因此,只有在版本更改后才会调用 Upgrade 方法,并且仅调用一次(因为您通过设置 UpgradeRequired = false 来禁用进一步升级,直到版本更改 -当属性恢复默认值 true 时)。

A few clarifications:

You have to call the Upgrade method of ApplicationSettingsBase derived class (that is normally called Settings and is created for you by Visual Studio):

Properties.Settings.Default.Upgrade();

When/where to call the Upgrade method? There is a simple trick you can apply: define a user setting called UpgradeRequired (example) as bool (the easiest way is through IDE). Make sure its default value is true.

Insert this code snipped at the start of the application:

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

So the Upgrade method will be called only after the version changes and only one time (since you disable further upgrades by setting UpgradeRequired = false until a version change - when the property regains default value of true).

我爱人 2024-09-02 00:24:03

马库斯·奥尔森(Markus Olsson)已经给出了一个很好的答案这里

本质上,您需要使用 ApplicationSettingsBase.Upgrade()< /a> 方法。

Markus Olsson has already given a pretty good answer here.

Essentially you will need to use the ApplicationSettingsBase.Upgrade() method.

阳光的暖冬 2024-09-02 00:24:03

我希望其他人有更好的答案。几年前我遇到了这个问题,我能找到的唯一解决方案(确实有效)是使用我自己的机制来存储设置,而不是默认的内置 .NET 方式。

I hope someone else has a better answer. I had this question a few years ago, and the only solution I could find (which did work) was to use my own mechanism for storing settings, rather than the default built-in .NET way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文