How To Change app.config Data

The code given below is used to change app.config data at runtime.
  1. private static void SetSetting(string key, string value)  
  2.        {  
  3.            Configuration configuration =  
  4.                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
  5.            configuration.AppSettings.Settings[key].Value = value;  
  6.            configuration.Save(ConfigurationSaveMode.Full, true);  
  7.            ConfigurationManager.RefreshSection("appSettings");  
  8.        }  
The code given below is used to fetch app.config data.
  1. private static string GetSetting(string key)  
  2.         {  
  3.             return ConfigurationManager.AppSettings[key];  
  4.         }  
App.config file



Key is lang and value is English.
 
Output

We get the "lang" value as English, this value is fetched from App.config file.



Modify App.config value at runtime.
 
  
Code
  1. public Form1()  
  2. {  
  3.     InitializeComponent();   
  4.     string objAppConfigValue = GetSetting("lang");  
  5.     SetSetting("lang""Tamil");  
  6.     string objModifiedAppConfigValue = GetSetting("lang");  
  7.      
  8. }  

  9. private static string GetSetting(string key)  
  10. {  
  11.     return ConfigurationManager.AppSettings[key];  
  12. }  

  13. private static void SetSetting(string key, string value)  
  14. {  
  15.     Configuration configuration =  
  16.         ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
  17.     configuration.AppSettings.Settings[key].Value = value;  
  18.     configuration.Save(ConfigurationSaveMode.Full, true);  
  19.     ConfigurationManager.RefreshSection("appSettings");  
  20. }  
Ebook Download
View all
Learn
View all