In addition to the answer by fenix2222 (which worked for me) I had to modify the last line to:
config.Save(ConfigurationSaveMode.Modified);
Without this, the new value was still being written to the config file but the old value was retrieved when debugging.