.net 动态刷新 app.config [英] .net dynamically refresh app.config

查看:37
本文介绍了.net 动态刷新 app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 .net Windows 应用程序中动态重新加载 app.config?我需要动态地打开和关闭登录,而不仅仅是基于应用程序启动时的值.

How do I dynamically reload the app.config in a .net Windows application? I need to turn logging on and off dynamically and not just based upon the value at application start.

ConfigurationManager.RefreshSection("appSettings") 不起作用,我还尝试使用 OpenExeConfiguration 显式打开配置文件,但我总是在应用程序启动时获取缓存值,而不是当前值.

ConfigurationManager.RefreshSection("appSettings") does not work and I've also tried explicitly opening the config file using OpenExeConfiguration but I always get the cached value at application startup and not the current value.

我已经接受了创建自定义配置部分的答案.作为旁注和愚蠢的错误 - 如果您从 IDE 运行,则更新 app.config 文件并期待更改毫无意义.您必须修改 bin\debug 文件夹中的 .exe.config 文件.呸!

I've accepted the answer of creating a custom configuration section. As a side note and foolish mistake - if you're running from the IDE there's no point in updating the app.config file and expecting changes. Yuo have to modify the .exe.config file in the bin\debug folder. Doh!

推荐答案

您可以按照您所说的方式刷新您自己的部分:

You can refresh your own section the way you say:

ConfigurationManager.RefreshSection("yoursection/subsection");

只需将日志记录的真/假移动到一个部分,你就可以了.

Just move a logging true/false into a section and you'll be fine.

这篇关于.net 动态刷新 app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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