0

If I have in the web.config file of web application :

<appSettings>
    <add key="DD" value="567_Access"/>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
</appSettings>

I know how to read the data in appSettings sections like this :

 string accessD = ConfigurationManager.AppSettings["DD"];

But I want to know how to Modify (Set) the value of a key in appSettings Through the code ?

(I want through a specific check to this value to stop the application under some circumstances)

Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
  • 1
    http://stackoverflow.com/questions/719928/how-do-you-modify-the-web-config-appsettings-at-runtime – Alex Dec 23 '16 at 13:27

1 Answers1

2
System.Configuration.ExeConfigurationFileMap configFile = new System.Configuration.ExeConfigurationFileMap();
configFile.ExeConfigFilename = "ConsoleTester.exe.config";  //name of your config file, can be from your app or external
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFile, System.Configuration.ConfigurationUserLevel.None);
System.Configuration.KeyValueConfigurationCollection settings = config.AppSettings.Settings;
settings["DD"].Value = "007_Access";
config.Save();
Gregor Primar
  • 6,759
  • 2
  • 33
  • 46