gpt4 book ai didi

.net-2.0 - 我可以控制 .NET 用户设置的位置以避免在应用程序升级时丢失设置吗?

转载 作者:行者123 更新时间:2023-12-03 04:49:08 25 4
gpt4 key购买 nike

我正在尝试自定义 user.config 文件的位置。目前它存储有哈希值和版本号

%AppData%\[CompanyName]\[ExeName]_Url_[some_hash]\[Version]\

我希望它与应用程序的版本无关

%AppData%\[CompanyName]\[ProductName]\

这可以做到吗?如何做到?有何影响?升级后用户会丢失之前版本的设置吗?

最佳答案

我想添加这段引用的文字作为将来遇到此问题时的引用。据说您可以通过调用 Upgrade 来指示 ApplicationSettings 基础结构从以前的版本复制设置。 :

Properties.Settings.Value.Upgrade();

来自Client Settings FAQ博客文章:( archive )

Q: Why is there a version number in the user.config path? If I deploy a new version of my application, won't the user lose all the settings saved by the previous version?

A: There are couple of reasons why the user.config path is version sensitive.

(1) To support side-by-side deployment of different versions of an application (you can do this with Clickonce, for example). It is possible for different version of the application to have different settings saved out.

(2) When you upgrade an application, the settings class may have been altered and may not be compatible with what's saved out, which can lead to problems.

However, we have made it easy to upgrade settings from a previous version of the application to the latest. Simply call ApplicationSettingsBase.Upgrade() and it will retrieve settings from the previous version that match the current version of the class and store them out in the current version's user.config file. You also have the option of overriding this behavior either in your settings class or in your provider implementation.

Q: Okay, but how do I know when to call Upgrade?

A: Good question. In Clickonce, when you install a new version of your application, ApplicationSettingsBase will detect it and automatically upgrade settings for you at the point settings are loaded. In non-Clickonce cases, there is no automatic upgrade - you have to call Upgrade yourself. Here is one idea for determining when to call Upgrade:

Have a boolean setting called CallUpgrade and give it a default value of true. When your app starts up, you can do something like:

if (Properties.Settings.Value.CallUpgrade)
{
Properties.Settings.Value.Upgrade();
Properties.Settings.Value.CallUpgrade = false;
}

This will ensure that Upgrade() is called only the first time the application runs after a new version is deployed.

我根本不相信它真的可以工作 - 微软不可能提供这种能力,但方法是一样的。

关于.net-2.0 - 我可以控制 .NET 用户设置的位置以避免在应用程序升级时丢失设置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/621265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com