title: How to Update (Add/Modify/Delete) keys in AppSettings section of web.config at runtime
body: <p>I like to Update keys/Values defined in <code>AppSettings</code> section of <code>Web.config</code> at runtime. however I DO NOT want to actually save them to <code>Web.config</code> file.</p>
  
  <p>I have a huge web application that have consists of many modules, DLLs and source code files. A bunch of critical information ranged from database configuration, encryption keys, username and passwords for webservices are saved in <code>AppSettings</code> section of the <code>web.config</code> file. Recent project requirement needs me to move these values out of <code>web.config</code> and keep in a secure storage.</p>
  
  <p>I already secured these values in an external location and I can read them back when application starts.</p>
  
  <p>here is the sample code.</p>
  
  <p><strong>Global.asax</strong></p>
  
  <pre><code>public class Global: System.Web.HttpApplication {
      protected void Application_Start(object sender, EventArgs e) {
          Dictionary&lt;string, string&gt; secureConfig = new Dictionary&lt;string,string&gt;{};
  
          // --------------------------------------------------------------------
          // Here I read and decrypt keys and add them to secureConfig dictionary
          // To test assume the following line is a key stored in secure sotrage.
          //secureConfig = SecureConfig.LoadConfig();
          secureConfig.Add("ACriticalKey","VeryCriticalValue");
          // --------------------------------------------------------------------
  
          foreach (KeyValuePair&lt;string, string&gt; item in secureConfig) {
              ConfigurationManager.AppSettings.Add(item.Key, item.Value);
          }
      }
  }
  </code></pre>
  
  <p>As you may noticed it is not feasible to change references to <code>AppSettings</code> in a massive code created by multiple programming teams to read their settings from my <code>secureConfig dictionary</code> and on the other hand I should not save these values in <code>web.config</code> file which is available to web administrators and operators, system admins and cloud admins.</p>
  
  <p>To Make programmers life easier, I want to let them add their values to <code>AppSettings</code> section of <code>web.config</code> during development, but they will be removed from there and put to secure storage later during deployment, however these values should be available to program transparently as they are still in <code>AppSettings</code> section.</p>
  
  <p><strong>Question</strong>: how can I add values to <code>AppSettings</code> at runtime so program can read them using <code>ConfigurationManager.AppSettings["ACriticalKey"]</code> to get <code>"VeryCriticalValue"</code> without saving them in Web.Config?</p>
  
  <p><strong>Please note</strong>: <code>ConfigurationManager.AppSettings.Add(item.Key, item.Value);</code> gives me <code>ConfigurationErrorsException</code> with message <code>The configuration is read only.</code></p>
  
  <p><strong>Please note</strong>: Preferably some settings should be able to stay in <code>AppSettings</code> as before</p>