1

I have been working with Console applications before and using App.config. Now I am trying to create WPF tool and went with App.config as well. However while testings I have noticed strange behavior and later discovered that after writing to App.config application should be closed and opened again so that changes in App.config will take in place.

I have a method for updating values in App.config:

    private static void UpdateSetting(string key, string value)
    {
        Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        configuration.AppSettings.Settings[key].Value = value;
        configuration.Save();

        ConfigurationManager.RefreshSection("appSettings");
    }

And button:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        UpdateSetting("MinutesExecution", MinutesToWait.Text);

        MessageBox.Show("Your changes has been saved!");
    }

Is there any other way so that my changes to settings will be taken in consideration without closing an application? I know I can create separate file and read from there but is there any built in function that can be used in Visual Studio like App.config?

10101
  • 2,232
  • 3
  • 26
  • 66
  • Look into FileSystemWatcher. Example for app.config [here](https://stackoverflow.com/questions/13876372/using-filesystemmonitoring-for-reading-changes-in-app-config-and-writing-to-app) documentation [here](https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=netcore-3.1) – Mikael Jun 10 '20 at 19:06
  • If you install an app it will usually end up in program files, where your users cannot write to files. You should therefore persist user settings into appdata. Dynamic settings can be applied using the dynamicresource mechanism. You could persist as a flat uncompiled resource dictionary, merge changes into application.current.resources as they are made and merge your resource dictionary as your app starts. – Andy Jun 11 '20 at 16:49

0 Answers0