在调试会话之间保留 app.config 中的数据 [英] Persisting the data in app.config between debugging sessions

查看:20
本文介绍了在调试会话之间保留 app.config 中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,长话短说,我正在开发一个应用程序,该应用程序将利用一些可能在运行时通过应用程序本身更改的配置信息.为此,我曾考虑使用 Settings 类.

So, long story short, I'm developing an application that will make use of some configuration info that may be changed at runtime through the application itself. For the purpose I've thought of using the Settings class.

问题是,信息没有在应用程序的不同运行之间持久化:

The problem, thought, is that information is not persisted between different runs of the application:

运行 1)

Console.WriteLine(Settings.Default["User"]); //prints "Default user"
Settings.Default["User"] = "abc";
Console.WriteLine(Settings.Default["User"]); //prints "abc"

运行 2)

Console.WriteLine(Settings.Default["User"]); //prints "Default user"
Settings.Default["User"] = "abc";
Console.WriteLine(Settings.Default["User"]); //prints "abc"

(两者都打印完全相同的输出)

两次运行都显示相同的第一次打印默认用户",尽管在第二次运行时我想得到abc",表明信息没有在不同的应用程序执行之间保持不变.

Both runs show up the same first print "Default user", although on the 2nd run I'd like to get "abc", indicating that the info is not being persisted between different application executions.

我承认这一定与 Visual Studio 处理 .config 文件的方式有关,但即便如此,我还是想知道如何纠正这种(讨厌的)行为?

I acknowledge this must be related with the way Visual Studio handles .config files, but even so I'd like to know how to correct for this (nasty) behavior?

推荐答案

默认情况下,App.config 不会直接复制,而是将其内容放在 .config 文件中在输出文件夹中.复印设置不适用于此操作.

By default, App.config is not copied directly, rather it's content is placed in <assembly-name>.config file in output folder. Copy settings do not apply to this operation.

通常,应用程序更改其自己的 app.config 并不是一个好习惯.如果您正在开发可能由同一台 PC 上的多个用户使用的应用程序,则 改用设置.这样每个用户都可以有自己的设置.

Generally, it is not a good practice for application to change its own app.config. If you are developing application that may be used by several users on the same PC, then use Settings instead. That way each user can have his own settings.

对于服务和系统范围的设置,请考虑使用其他存储,例如单独的配置文件、注册表或数据库.

For services and system-wide settings, consider using another storage, like a separate config file, registry or database.

编辑保存设置:

使用设置类时,应调用Save()将其写入文件,否则应用程序关闭时设置的更改将被丢弃.如果您在开发过程中经常终止您的应用程序,并且它没有到达它的结束代码(您通常会调用 Save()),那么您有几个选择:

When using settings class, you should call Save() to write it to the file, otherwise changes in settings will be discarded when application is closed. If you often terminate your application during development, and it does not reach it's end code(where you would normally place a call to Save()), then you have several options:

  1. 使用调试器监视窗口调用 Save().为此,请在监视窗口中放置一个类似于 Settings.Default.Save() 的表达式,并在每次要保存时刷新它.
  2. 您可以尝试使用计时器每秒调用一次 Save.
  3. 您可以在设置更改后在代码中插入 Save() 调用.
  4. 您可以编写自定义设置提供程序或包装器,以便在每次更改后立即保存设置.
  1. Use debugger watch window to call Save(). To do that, place an expression like Settings.Default.Save() in watch window and refresh it every time you want to save.
  2. You can try using a timer to call Save every second.
  3. You can insert Save() calls in your code after settings change.
  4. You can write custom Settings provider or wrapper that will immediately save settings after every change.

这篇关于在调试会话之间保留 app.config 中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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