2

Problem:

I have a program where i write Keys+Values in my appconfig during runtime, but when i want to read them i get the old values and the only way to get the new values is by restarting the application.

As soon as i write the key+value programmically the config file is updated so that is not the problem, but i cannot figure out why i wont get the new values during the same runtime.

I write like this: (tried with and without RefreshSection(key) - made no difference)

public static void AddValue(string key, string value)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location);
    config.AppSettings.Settings.Add(key, value);
    config.Save(ConfigurationSaveMode.Full);
    ConfigurationManager.RefreshSection(key);
}

And i read it like this:

string[] ItemsArray = ConfigurationManager.AppSettings["Items"].Split(',');

Question:

How can i read the new keys (in runtime) which i added during the same runtime?

XPtoLevel5
  • 23
  • 4
  • Why use the AppSettings? UserSettings should be much easier to use: https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings – Ranjith Venkatesh Jul 24 '18 at 12:52
  • Be aware that [ConfigurationManager.AppSettings.Settings.Add appends value on each run](https://stackoverflow.com/questions/31994302/configurationmanager-appsettings-settings-add-appends-value-on-each-run) – Renatas M. Jul 24 '18 at 13:03
  • @Reniuz yes i know and that is fine :D + at Ranjith: well it suits my needs but thank you :) – XPtoLevel5 Jul 24 '18 at 13:19

1 Answers1

1

You should try

ConfigurationManager.RefreshSection("appSettings");

Found an old post here Reloading configuration without restarting application using ConfigurationManager.RefreshSection

override
  • 36
  • 3