为什么在运行时不考虑我在App.config中对AppSettings所做的更改? (控制台应用程序) [英] Why my changes of AppSettings in App.config is not taken into account in run-time? (Console Application)

查看:157
本文介绍了为什么在运行时不考虑我在App.config中对AppSettings所做的更改? (控制台应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序,它具有自己的App.config。

I have a console application which has its own App.config.

我需要在部分时间更改某些值。

I need to change some values in section time to time.

我的问题是,当我在bin / debug文件夹中执行exe时,它会正确获取相关的appsettings。但是当我编辑和更改某些键/值对的值并重新运行exe时,它仍会读取原始值。

My problem is, when I execute the exe within the bin/debug folder it gets the relevant appsettings correctly. But when I edit and change values of some key/value pairs and RE-RUN the exe, it still reads the original values.

(通过重新运行,我的意思是通过调用MyTool.exe在命令提示符下运行应用程序)

(By RE-RUN I mean running the application on the command promt by calling MyTool.exe)

我尝试调用

ConfigurationManager.RefreshSection("appSettings");

在我的Main方法的开始。但是没有帮助。

in the begining of my Main method. But did not help.

可以请教吗?
谢谢

Can you please advise? Thanks

推荐答案


但是当我编辑和更改某些键/值对的值时并重新运行
exe,它仍会读取原始值。

But when I edit and change values of some key/value pairs and RE-RUN the exe, it still reads the original values.

取决于您如何重新运行此exe 。如果在Visual Studio中执行此操作,则通过按 F5 ,VS只需将项目中的app.config文件复制到输出中,然后将其重命名为 AppName.exe.config 。因此,如果要考虑更改,则必须修改 AppName.exe.config (而不是 App.config ),然后从Windows资源管理器中运行可执行文件。

Depends how you are RE-RUNNing this exe. If you are doing this in Visual Studio, by hitting F5, VS simply copies the app.config file in your project to the output and renames it to AppName.exe.config. So if you want your changes to be taken into account you have to modify AppName.exe.config (not App.config) and then run the executable from the Windows Explorer.

可以这样说,App.config仅读取和解析一次。应用程序启动时。然后将这些值缓存,以避免在您的应用程序每次请求某个值时进行昂贵的XML解析。

This being said, the App.config is read and parsed only once. When the application starts. The values are then cached to avoid expensive XML parsing everytime your application requests some value.

App.config设计为存储不应更改的配置值。如果需要动态更改配置值,则应该使用其他存储机制:文件,数据库,...

App.config is designed to store configuration values that are not supposed to be changed. If you need to change configuration values dynamically you should use some other storage mechanism: file, database, ...

但是 ConfigurationManager.RefreshSection( appSettings); 方法应该起作用。修改 AppName.exe.config 文件后,调用此方法,然后使用 ConfigurationManager.AppSettings [ someKey ]; 应该会向您返回新值。

But the ConfigurationManager.RefreshSection("appSettings"); method should work. Once you have modified the AppName.exe.config file, you call this method and then refetch the value you need using ConfigurationManager.AppSettings["someKey"]; which should return you the new value.

这篇关于为什么在运行时不考虑我在App.config中对AppSettings所做的更改? (控制台应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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