This article will explore the details related to change the App.Config file setting at runtime in Windows application.
Introduction
Sometimes we require to change the settings related to App.Config file at runtime in the application. So, we require the AppSettings for changing the App.Config File
Pre-Requisite: User should know how to add the appSettings in the App.Config fie, so user should have basic idea to create the key and set the value for the key in the AppSetting node before changing the value at runtime.
Required references
We require following the references in the application to modify the App.Config File1. System //This is the default reference
2. System.Configuration

All Set? Now change the Config File
After adding the details related to the References we can change the AppSetting section from the App.Config file
Now, add the namespace
System.Configuration.
Code to change the AppSetting section.
C# Code
//Create the object of the Class Configuration
Configuration objConfig= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//Key to Add in the App.Config file
objConfig.AppSettings.Settings.Add("CurrentYear", DateTime.Now.Year);
// Now, we can save the configuration file.
objConfig.Save(ConfigurationSaveMode.Modified);
// After saving the file we have to refresh the section
ConfigurationManager.RefreshSection("appSettings");
NOTE:
You will not see any changes in the App.Config file as it may be in the debug mode. So please release the application and make the changes you can see the change in the app.config file
Conclusion
This way you can manage the appSetting section of the App.Config file from the C# application at runtime.

About the Author