Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change appconfig on runtime

Tags:

c#

winforms

I am trying to update the appconfig file on run time with the following code. I do not get an error but it does not update my config file.

System.Configuration.Configuration config =
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string oldValue = config.AppSettings.Settings["Username"].Value;
config.AppSettings.Settings["Username"].Value = "NewValue";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
like image 650
user1292656 Avatar asked Jun 17 '26 03:06

user1292656


1 Answers

Adding the config.AppSettings.SectionInformation.ForceSave = true; will do the trick. You should then look in YourProject.vshost.exe.config when debugging as Justin said. The modifications are saved there.

like image 165
Stacked Avatar answered Jun 18 '26 15:06

Stacked