Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App Config Modification not reflected in code

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 ?

like image 660
Rajeev Kumar Avatar asked Jun 26 '26 06:06

Rajeev Kumar


2 Answers

The reason behind why the AppSetting section in app.config file is not getting reflected during update in Run time is as follows:

  • When you add a new app.config file it actually creates a file in the local system.
  • When you compile it, actually it creates the necessary files including .Exe files in Debug/Release folder; depending upon the Build mode.
  • After successful build it also generates a .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.
  • So whenever you edit the 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.

like image 76
Santosh Panda Avatar answered Jun 27 '26 21:06

Santosh Panda


I have my own answer. Just need to refresh appSettings section as

ConfigurationManager.RefreshSection("appSettings");
like image 30
Rajeev Kumar Avatar answered Jun 27 '26 21:06

Rajeev Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!