更改程序集/文件版本后保留用户设置 [英] Keep user's settings after altering assembly/file version

查看:85
本文介绍了更改程序集/文件版本后保留用户设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我有一个用C#编写的简单WinForms应用程序。我讨论了部署解决方案一段时间,但最终决定放弃ClickOnce,因为其中的一些限制至关重要。

Background
I have a simple WinForms application written in C#. I debated deployment solutions for a while but ultimately decided to abandon ClickOnce as a few of the constraints were crucially restrictive.

相反,我改编了一个简单的解决方案通过应用程序属性中的程序集/文件版本(我保持它们同步)。我正在通过安装项目(* .msi)进行部署。我将最新的程序集版本号以及最新的安装程序文件存储在在线XML文件中。在运行时,我只需要在线检查Application.ProductVersion的最新版本,然后打开更新对话框(如果有可用更新)。

Instead, I've adapted a simple solution of versioning the application via the assembly/file versions (I keep them in sync) in the application's properties. I'm deploying via a Setup Project (*.msi). I store the latest assembly version number in an XML file online, as well as the latest installer file. At run-time, I simply check the Application.ProductVersion against the latest version online and open an update dialog if an update is available.

问题 >

到目前为止,这种方法效果很好,但是我最近注意到这种方法存在一个主要问题。更新应用程序的程序集版本后,会在AppData / Company / Product / Version / blahblahblah中创建用户设置文件(user.config)的新版本。显然,这迫使用户重置新版本中的所有内容。

Problem
This has worked pretty well thus far, but I've recently noticed a major problem with this approach. When the assembly version of the application is updated, a new version of the user's settings file (user.config) is created in AppData/Company/Product/Version/blahblahblah. This obviously forces the user to reset everything in the new version.

建议的解决方案

我不确定如何继续。到目前为止,该应用程序只有1个发行版,当前的用户群基本上是我可以请求对其进行测试的人,因此切换策略没什么大不了的。我考虑过:

Suggested Solutions
I'm not sure how to proceed. The application only has 1 release thus far and the current user base is basically whoever I can beg to test it, so switching up strategies is no big deal. I've considered:

1。)编写我自己的设置系统,从而可以完全控制设置文件的存储和使用位置。

2.)重新考虑我的版本控制/更新策略,以使更新不基于程序集版本。我不确定该怎么做,但是我的测试似乎表明,即使使用相同的程序集版本构建和安装新版本仍然会破坏user.config。

1.) Write my own settings system and thus have complete control over where/how the settings file is stored and used.
2.) Re-think my versioning/update strategy so that the update is not based on the assembly version. I'm not sure how I would do this, but my testing seemed to reveal that even building and installing a new version with the same assembly version would still break user.config.

我想我真正要问的是,是否有任何方法可以保留默认设置系统,因为它既易于使用,又可以根据我的部署策略进行调整。

I guess what I'm truly asking if there is any way to preserve the default settings system since it's so easy to use while also adapting it to my deployment strategy.

推荐答案

使用内置的Settings类,您只需在更改应用程序版本时就升级设置。操作方法如下:
在Settings.settings文件中,创建一个新设置
UpdateSettings type = bool Scope = User Value = True

Use the built in Settings classes, you just need to upgrade the settings anytime you change the application version. Here's how to do it: In the Settings.settings file, create a new setting UpdateSettings type=bool Scope=User Value=True

在使用任何设置之前,请包含以下代码(它可以在应用每次运行时运行,因为这也使得在调试器中的运行更加容易)

Include the following code before you use any Settings (it can run every time the app runs, as this makes running in debugger easier too)

// Copy user settings from previous application version if necessary
if (MyApp.Properties.Settings.Default.UpdateSettings)
{
    MyApp.Properties.Settings.Default.Upgrade();
    MyApp.Properties.Settings.Default.UpdateSettings = false;
    MyApp.Properties.Settings.Default.Save();
}

运行新应用程序版本时,UpdateSettings的默认值为True,您的旧设置都不会被使用。如果UpdateSettings为true,则从旧设置升级设置,然后保存在新的应用程序版本下。

When your new application version is run UpdateSettings will have a default value of True and none of your old settings will be used. If UpdateSettings is true we upgrade the settings from the old settings and save then under the new app version.

这篇关于更改程序集/文件版本后保留用户设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆