为什么坚持用户设置不装? [英] Why are persisted user settings not loaded?

查看:101
本文介绍了为什么坚持用户设置不装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用存储在默认的应用程序设置一些配置设置的组件上的Windows应用程序。

I have a windows application that uses an assembly that stores some configuration settings in the default application settings.

的设置可以在运行时更改,因此被持久化

The settings can be changed in run time and are persisted thus:

Properties.Settings.Default.SelectedCOMPort = options.SelectedCOMPort;
Properties.Settings.Default.Save();



设置被正确保存和我通过查看保存在用户的user.config文件证实了这一点应用程序目录,例如

The settings are saved correctly and I confirm this by looking at the user.config file saved in the users application directory E.g.

C:\Documents and Settings\e399536\Local Settings\Application Data\MyCompany\MyTool

然而,当工具被关闭,然后再次开始的所有设置都含有其默认值。

However when the tool is closed and then started again all the settings are loaded with their default values.

检查user.config文件,一旦运行该应用程序确认的设置仍保存。

Checking the user.config file once the application is running confirms that the settings are still as saved.

这些设置这样加载:

options.SelectedCOMPort = Properties.Settings.Default.SelectedCOMPort;



为什么正在使用和默认设置不保存的?

Why are the default settings being used and not the saved ones?

有我错过了什么?

@ Tenaciouslmpy
组装的构造,这本身就是在加载时加载的设置主要组件的形式加载事件。

@ Tenaciouslmpy The settings are loaded during the constructor of the assembly, which itself is loaded in the form load event of the main assembly.

@奥斯汀
这是我在Visual Studio中正在调试一个独立的应用程序。

@ Austin This is a standalone app that I am debugging in Visual Studio.

推荐答案

如果你重新编译运行的应用程序,请注意,它会认为应用程序的新版本,将不可以自动负载每个用户设置。你需要调用Settings.Default.Upgrade在这种情况下。

If you are recompiling the application between runs, note that it will consider that a new version of the app and will not automatically load per-user settings. You need to call Settings.Default.Upgrade in this situation.

要做到这一点只在需要时的一种方法是一个NeedsUpgrade设置(True值)添加到应用程序的默认每用户设置。在应用程序启动时,检查是否NeedsUpgrade是真实的。如果是这样,叫升级,设置NeedsUpgrade为False,并保存设置。下一次应用程序版本的更改,NeedsUpgrade将重置为True,你会自动调用升级到任何现有的用户设置带来一次。

One way to do this only when needed is to add a NeedsUpgrade setting (value True) to the application's default per-user settings. On app startup, check if NeedsUpgrade is true. If so, call Upgrade, set NeedsUpgrade to False, and save the settings. The next time the app version changes, NeedsUpgrade will reset to True and you'll auto-call Upgrade to bring in any existing user settings again.

请确保你设置NeedsUpgrade 调用升级后,还是会得到一扫而光当设置升级。

Make sure you're setting NeedsUpgrade after calling Upgrade, or it will get wiped out when the settings are upgraded.

if (Settings.Default.NeedsUpgrade)
{
    Settings.Default.Upgrade();
    Settings.Default.NeedsUpgrade = false;
    Settings.Default.Save();
}

这篇关于为什么坚持用户设置不装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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