But when I edit and change values of some key/value pairs and RE-RUN
the exe, it still reads the original values.
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.
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 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, ...
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…