在.NET在不同的程序集版本你如何保持user.config设置? [英] How do you keep user.config settings across different assembly versions in .net?
问题描述
基本上,问题是,每一次(即用户安装应用程序的新版本)所有的设置与复位默认值(或更精确地创建一个新的user.config文件的文件夹中的程序集版本的变化不同的版本号作为名称)
Basically the problem is that each time the assembly version changes (i.e. the user installs a new version of the application) all their settings are reset the the defaults (or more accurately a new user.config file is created in a folder with a different version number as the name)
如何才能保持相同的设置升级版本时,因为使用INI文件或注册表似乎气馁?
How can I keep the same settings when upgrading versions, since using ini files or the registry seem to be discouraged?
当我们使用的ClickOnce它似乎能够处理这个问题,所以现在看来似乎应该可以做,但我不知道怎么样。
When we used Clickonce it seemed to be able to handle this, so it seems like it should be able to be done, but I'm not sure how.
推荐答案
ApplicationSettingsBase有一个<一个href="http://msdn.microsoft.com/en-us/library/system.configuration.applicationsettingsbase.upgrade.aspx">method所谓的升级其迁移所有设置从previous版本。
ApplicationSettingsBase has a method called Upgrade which migrates all settings from the previous version.
为了运行,只要你发布你的应用程序的新版本合并,你可以定义一个布尔标志在你的设置文件默认为true。将其命名为 UpgradeRequired 或类似的东西。
In order to run the merge whenever you publish a new version of your application you can define a boolean flag in your settings file that defaults to true. Name it UpgradeRequired or something similar.
然后,在应用程序启动您检查是否设置了标志,如果是,调用<一href="http://msdn.microsoft.com/en-us/library/system.configuration.applicationsettingsbase.upgrade.aspx">Upgrade法,将该标志设置为false,并保存配置。
Then, at application start you check to see if the flag is set and if it is, call the Upgrade method, set the flag to false and save your configuration.
if (Settings.Default.UpgradeRequired)
{
Settings.Default.Upgrade();
Settings.Default.UpgradeRequired = false;
Settings.Default.Save();
}
了解更多有关升级方法的<一个href="http://msdn.microsoft.com/en-us/library/system.configuration.applicationsettingsbase.upgrade.aspx">MSDN.该<一href="http://msdn.microsoft.com/en-us/library/system.configuration.applicationsettingsbase.get$p$pviousversion.aspx">Get$p$pviousVersion也可能是值得一试,如果你需要做一些定制的融合。
Read more about the Upgrade method at MSDN. The GetPreviousVersion might also be worth a look if you need to do some custom merging.
这篇关于在.NET在不同的程序集版本你如何保持user.config设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!