windows 为什么未加载持久用户设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2201819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:53:15  来源:igfitidea点击:

Why are persisted user settings not loaded?

c#windowswinformspersistencesettings

提问by Kildareflare

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

我有一个使用程序集的 Windows 应用程序,该程序集在默认应用程序设置中存储一些配置设置。

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

这些设置可以在运行时更改并保持不变:

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

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

设置已正确保存,我通过查看保存在用户应用程序目录中的 user.config 文件来确认这一点,例如

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.

但是,当该工具关闭然后再次启动时,所有设置都将以其默认值加载。

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

在应用程序运行后检查 user.config 文件确认设置​​仍然保存。

The settings are loaded thus:

设置是这样加载的:

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

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

为什么使用默认设置而不是保存的设置?

Have I missed something??

我错过了什么吗?

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

@Tenaciouslmpy 设置是在程序集的构造函数期间加载的,它本身是在主程序集的表单加载事件中加载的。

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

@ Austin 这是我在 Visual Studio 中调试的独立应用程序。

回答by technophile

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

如果您在两次运行之间重新编译应用程序,请注意它会考虑应用程序的新版本,并且不会自动加载每个用户的设置。在这种情况下,您需要调用 Settings.Default.Upgrade。

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 设置(值为 True)添加到应用程序的默认每用户设置中。在应用程序启动时,检查 NeedsUpgrade 是否为 true。如果是,请调用 Upgrade,将 NeedsUpgrade 设置为 False,然后保存设置。下次应用版本更改时,NeedsUpgrade 将重置为 True,您将自动调用 Upgrade 以再次引入任何现有用户设置。

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

确保调用 Upgrade设置 NeedsUpgrade ,否则设置升级它会被清除。

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

回答by Austin Salonen

This sounds like you're debugging the application from Visual Studio when every time you start a new session you start with the default data.

这听起来像是在每次启动新会话时都使用默认数据从 Visual Studio 调试应用程序。

If you're seeing this with an installed release, then I would guess you're not actually using the string values when you think you are.

如果您在已安装的版本中看到这一点,那么我猜您实际上并没有在您认为的时候使用字符串值。