0

I have these pieces of code:

string theme = ConfigurationManager.AppSettings["Theme"];


private void ChangeTheme(string Name)
    {
        if(Name=="Light")
        {
            Form1.ActiveForm.BackColor = System.Drawing.Color.White;
            Form.ActiveForm.ForeColor = System.Drawing.Color.Black;
        }
        if (Name == "Dark")
        {
            Form1.ActiveForm.BackColor = System.Drawing.Color.Black;
            Form.ActiveForm.ForeColor = System.Drawing.Color.DarkOrange;
        }
        Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        cfg.AppSettings.Settings["Theme"].Value = Name;
        cfg.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
    }

My app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Volume" value="7"/>
    <add key="Keyval" value="X"/>
    <add key="Theme" value="Light"/>
  </appSettings>
</configuration>

Basically, when I press the radio buttons it changes the theme and sends the string to changetheme(), but it does not update in the app.config.

dvs
  • 97
  • 1
  • 8
  • 1
    Possible duplicate of [How to modify my App.exe.config keys at runtime?](http://stackoverflow.com/questions/5468342/how-to-modify-my-app-exe-config-keys-at-runtime) – Igor Apr 01 '16 at 18:08
  • Possible duplicate of http://stackoverflow.com/questions/11149556/app-config-change-value – David Oesterreich Apr 01 '16 at 18:59
  • Why you don't use a [`Settings.settings`](https://msdn.microsoft.com/en-us/library/0zszyc6e(v=vs.110).aspx) ? – Reza Aghaei Apr 01 '16 at 19:05

2 Answers2

0

Are you running it in debug?

Probably you are looking at the wrong file. While debugging visual studio uses the config file from the bin\Debug so it would not update the App.config from the solution.

0

Instead of using == for comparison, I would use the String Equals Method:

Name.Equals("Light")

While I'd bet that it's not the issue, it's good practice and maybe you get lucky and that is the issue.

user640118
  • 803
  • 2
  • 13
  • 25