Reputation: 4963
I have a appsetting section in appconfig file as
<appSettings>
<add key="DayTime" value="08-20"/>
<add key="NightTime" value="20-08"/>
</appSettings>
I am tyring to modify the app config while application is running. I changed key DayTime
to 11-20 while application is running.
Now if I run this code again to fetch data from config, it shows previous set values.
private void btnDayNightSettings_ShowingEditor(object sender, ItemCancelEventArgs e)
{
string[] strDayTime = ConfigurationManager.AppSettings["DayTime"].Split('-');
}
Why it is so ?
Upvotes: 3
Views: 16007
Reputation: 7341
The reason behind why the AppSetting section
in app.config
file is not getting reflected during update in Run time is as follows:
.Exe
files in Debug/Release
folder; depending upon the Build mode..config
file which looks like YourApplicationName.exe.config
which holds the same entries in original app.config file. And the .Exe
always refers to this file.app.config
at Runtime it actually updates the file but changes are not updated in YourApplicationName.exe.config
file as it has not re-build yet.So every time you need to re-build to your app to reflect the changes.
Upvotes: 15
Reputation: 4963
I have my own answer. Just need to refresh appSettings
section as
ConfigurationManager.RefreshSection("appSettings");
Upvotes: 6
Reputation: 3752
app.config is cached, the changes will reflect when you restart the application. See is app.config file in WinForms cached by .Net framework?
Upvotes: 2
Reputation: 2709
Try this:
string strDayTime = ConfigurationManager.AppSettings["DayTime"];
ConfigurationManager.AppSettings.Set("DayTime", "11-20");
strDayTime = ConfigurationManager.AppSettings["DayTime"];
The value of the strDayTime variables changes on both lines.
Upvotes: 0