C# Write and read config file

app.config

  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.   <appSettings>  
  4.     <add key="oldPlace" value="4" />  
  5.   </appSettings>  
  6. </configuration>  

Write:


  1. System.Configuration.Configuration config =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
  2. config.AppSettings.Settings["oldPlace"].Value = "3";       
  3. config.Save(ConfigurationSaveMode.Modified);  
  4. ConfigurationManager.RefreshSection("appSettings");  

Read:

  1. string value = ConfigurationManager.AppSettings["oldPlace"];  

No comments:

Post a Comment