如何在文件版本更改期间保留 .settings/.config 文件中的更改?
我创建了一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一些说明:
您必须调用
ApplicationSettingsBase
派生类的Upgrade
方法(通常称为Settings
,由 Visual Studio 为您创建) ):何时何地调用
Upgrade
方法?您可以应用一个简单的技巧:将名为UpgradeRequired
(示例)的用户设置定义为bool
(最简单的方法是通过 IDE)。确保其默认值为true
。在应用程序的开头插入这段代码:
因此,只有在版本更改后才会调用 Upgrade 方法,并且仅调用一次(因为您通过设置
UpgradeRequired = false
来禁用进一步升级,直到版本更改 -当属性恢复默认值true
时)。A few clarifications:
You have to call the
Upgrade
method ofApplicationSettingsBase
derived class (that is normally calledSettings
and is created for you by Visual Studio):When/where to call the
Upgrade
method? There is a simple trick you can apply: define a user setting calledUpgradeRequired
(example) asbool
(the easiest way is through IDE). Make sure its default value istrue
.Insert this code snipped at the start of the application:
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 oftrue
).马库斯·奥尔森(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.
我希望其他人有更好的答案。几年前我遇到了这个问题,我能找到的唯一解决方案(确实有效)是使用我自己的机制来存储设置,而不是默认的内置 .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.