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

发布于 2024-07-14 14:20:37 字数 302 浏览 6 评论 0 原文

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

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

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

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

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

I'm trying to customize the location of the user.config file. Currently it's stored with a hash and version number

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

I want to it be agnostic to the version of the application

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

Can this be done and how? What are the implications? Will the user lose their settings from the previous version after upgrading?

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

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

发布评论

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

评论(4

夜还是长夜 2024-07-21 14:20:37

我想添加这段引用的文字作为将来遇到此问题时的参考。 据说您可以通过调用 升级

Properties.Settings.Value.Upgrade();

来自客户端设置常见问题解答博客文章:< em>(存档

问:为什么user.config路径中有版本号? 如果我部署应用程序的新版本,用户是否会丢失先前版本保存的所有设置?

答:有几个原因
user.config 路径是版本敏感的。

(1) 支持并行部署
的不同版本
应用程序(您可以使用
例如,单击一次)。 这是
可能适用于不同版本
应用程序有不同的设置
保存出来了。

(2) 当您升级
应用程序,设置类可以
已经被改变并且可能不会被改变
与保存的内容兼容,
这可能会导致问题。

但是,我们已经让您可以轻松地
升级之前的设置
应用程序的版本
最新的。 只需致电
ApplicationSettingsBase.Upgrade()
它将检索设置
之前的版本匹配
类和商店的当前版本
它们在当前版本中
用户.config 文件。 您还拥有
覆盖此行为的选项
在您的设置类中或在
您的提供商实现。

问:好的,但是我怎么知道什么时候该
调用升级?

答:好问题。 在 Clickonce 中,当
你安装了你的新版本
应用程序、应用程序设置库
会检测到并自动
立即为您升级设置
设置已加载。 在非 Clickonce 中
情况下,没有自动升级 -
你必须自己打电话升级。
这是确定何时的一个想法
调用升级:

有一个名为的布尔设置
调用Upgrade并给它一个默认值
真实的价值。 当您的应用程序启动时
起来,你可以这样做:

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

这将确保 Upgrade() 是
仅在第一次调用
应用程序在新版本后运行
已部署。

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

I wanted to add this quoted text as a reference for when i have this problem in the future. Supposedly you can instruct the ApplicationSettings infrastructure to copy settings from a previous version by calling Upgrade:

Properties.Settings.Value.Upgrade();

From Client Settings FAQ blog post: (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.

i don't believe for a second that it could actually work - there's no way Microsoft would provide this ability, but the method is there just the same.

霞映澄塘 2024-07-21 14:20:37

要回答第一个问题,从技术上讲,您可以将文件放在您想要的任何位置,但是您必须自己编写代码,因为文件所在的默认位置是两个示例中的第一个。 (链接到如何自己动手

至于第二个问题,这取决于你如何部署应用程序。 如果您通过 .msi 进行部署,则安装项目(msi 的构建基础)的属性中有两个哈希值:“升级代码”和“产品代码”。 这些决定了如何安装 msi,以及它是否升级、覆盖或安装在同一应用程序的任何其他版本旁边。

例如,如果您的软件有两个版本,并且它们具有不同的“升级”代码,那么对于 Windows,无论名称是什么,它们都是完全不同的软件。 但是,如果“升级”代码相同,但“产品”代码不同,那么当您尝试安装第二个 msi 时,它会询问您是否要升级,此时应该从旧配置到新配置。 如果两个值相同,并且版本号未更改,则新配置将位于与旧配置相同的位置,并且无需执行任何操作。 MSDN 文档

ClickOnce 有点不同,因为它更多地基于 ClickOnce 版本号和 URL 路径,但是我发现只要您继续“发布”到相同的版本location 新版本的应用程序将继续使用现有的配置。 (指向 ClickOnce 如何处理更新的链接

我也知道有一种方法可以在使用自定义安装脚本安装 msi 期间手动合并配置,但我不记得具体的步骤...(请参阅链接了解如何使用 web.config 进行此操作)

To answer the first question, you technically can put the file wherever you want, however you will have to code it yourself, as the default place the file goes to is the first of your two examples. (link to how to do it yourself)

As for the second question, it depends on how you deploy the application. If you deploy via a .msi, then there are two hashes in the properties of the setup project (that the msi is built from), the 'upgrade code' and the 'product code'. These determine how the msi can be installed, and if it upgrades, overwrites, or installs beside any other version of the same application.

For instance, if you have two versions of your software and they have different 'upgrade' codes, then to windows they are completely different pieces of software regardless of what the name is. However if the 'upgrade' code is the same, but the 'product' code is different then when you try to install the 2nd msi it will ask you if you want to upgrade, at which time it is supposed to copy the values from the old config to a new config. If both values are the same, and the version number didn't change then the new config will be in the same location as the old config, and it won't have to do anything. MSDN Documentation

ClickOnce is a little bit different, because its based more off of the ClickOnce version # and URL path, however I have found that as long as you continue to 'Publish' to the same location the new version of the application will continue to use the existing config. (link to how ClickOnce handles updates)

I also know there is a way to manually merge configs during the install of the msi using custom install scripts, but I don't remember the exact steps to do it... (see this link for how to do it with a web.config)

天暗了我发光 2024-07-21 14:20:37

user.config 文件存储在

C:\Documents and Settings>\\[Local Settings\]Application Data\\__< hash>\ <

C:\Documents and Settings> 是用户数据目录,可以是非漫游(上面的本地设置)或漫游。
是用户名。
是 CompanyNameAttribute 值(如果可用)。 否则,忽略此元素。
是 AppDomain.CurrentDomain.FriendlyName。 这通常默认为 .exe 名称。
是 URL、StrongName 或路径,基于可用于散列的证据。
是从 CurrentDomain 收集的证据的 SHA1 哈希值,按以下优先顺序排列:
1.强名
2.网址:
如果这些都不可用,请使用 .exe 路径。
是 AssemblyInfo 的 AssemblyVersionAttribute 设置。

完整说明位于此处 http://msdn.microsoft.com/en-us/库/ms379611.aspx

The user.config file is stored at

C:\Documents and Settings>\<username>\[Local Settings\]Application Data\<companyname>\<appdomainname>_<eid>_<hash>\<version>

<C:\Documents and Settings> is the user data directory, either non-roaming (Local Settings above) or roaming.
<username> is the user name.
<companyname> is the CompanyNameAttribute value, if available. Otherwise, ignore this element.
<appdomainname> is the AppDomain.CurrentDomain.FriendlyName. This usually defaults to the .exe name.
<eid> is the URL, StrongName, or Path, based on the evidence available to hash.
<hash> is a SHA1 hash of evidence gathered from the CurrentDomain, in the following order of preference:
1. StrongName
2. URL:
If neither of these is available, use the .exe path.
<version> is the AssemblyInfo's AssemblyVersionAttribute setting.

Full description is here http://msdn.microsoft.com/en-us/library/ms379611.aspx

云柯 2024-07-21 14:20:37

(我会将此作为评论添加到 @Amr 的答案,但我还没有足够的代表来执行此操作。)

MSDN 文章中的信息非常清楚,似乎仍然适用。 然而,它没有提到 SHA1 哈希值是以 32 进制编码写出的,而不是更典型的 16 进制。

我相信所使用的算法是在 ToBase32StringSuitableForDirName 中实现的,其中 可以在 Microsoft 参考源中找到

(I'd add this as a comment to @Amr's answer, but I don't have enough rep to do that yet.)

The info in the MSDN article is very clear and appears to still apply. However it fails to mention that the SHA1 hash is written out base 32 encoded, rather than the more typical base 16.

I believe the algorithm being used is implemented in ToBase32StringSuitableForDirName, which can be found here in the Microsoft Reference Source.

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