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

查看:121
本文介绍了在调试会话之间将数据保留在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,而是将其内容放置在输出文件夹中的<assembly-name>.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天全站免登陆