title: ConfigurationManager.AppSettings does not work with an App.config file created programmatically
body: <p>I ran into something interesting. I am trying to make a quick console app for our clients to run to change some connection strings on some of their programs. This being a &quot;quick app&quot; for clients, I want it to be &quot;idiot proof&quot;.</p>
  <p>One of the main things the app does is re-create a default config file should it not find it.</p>
  <pre><code> if (!File.Exists(&quot;./App.config&quot;))
          {
              Console.WriteLine(&quot;Config file is missing. Creating a new one now..&quot;);
              //file doesn't exist, let's create one.
              var doc = new XDocument(
                  new XDeclaration(&quot;1.0&quot;, &quot;UTF-16&quot;, null)
                  ,new XElement(&quot;configuration&quot;
                      , new XElement(&quot;startup&quot;
                          , new XElement(&quot;supportedRuntime&quot;, new XAttribute(&quot;version&quot;, &quot;v4.0&quot;)
                              , new XAttribute(&quot;sku&quot;, &quot;.NETFramework,Version=v4.5&quot;)))
                  , new XElement(&quot;appSettings&quot;
                      , new XElement(&quot;add&quot;, new XAttribute(&quot;key&quot;, &quot;targetDatabaseName&quot;), new XAttribute(&quot;value&quot;, &quot;&quot;))
                      , new XElement(&quot;add&quot;, new XAttribute(&quot;key&quot;, &quot;applicationServer&quot;), new XAttribute(&quot;value&quot;, &quot;&quot;))
                      , new XElement(&quot;add&quot;, new XAttribute(&quot;key&quot;, &quot;sqlServer&quot;), new XAttribute(&quot;value&quot;, &quot;&quot;))
                      , new XElement(&quot;add&quot;, new XAttribute(&quot;key&quot;, &quot;applicationServerPort&quot;), new XAttribute(&quot;value&quot;, &quot;&quot;))
                      , new XElement(&quot;add&quot;, new XAttribute(&quot;key&quot;, &quot;customSearchPath&quot;), new XAttribute(&quot;value&quot;, &quot;&quot;)))));
  
  
              using (var sw = new StringWriter())
              {
                  using (var xw = XmlWriter.Create(sw))
                  {
                      doc.Save(xw);
                      xw.Close();
                  }
  
                  doc.Save(&quot;./App.config&quot;);
                  sw.Close();
                  
              }
  
          }  
  </code></pre>
  <p>While testing I noticed that if the original App.config file is removed and re-created using the app, all of the <code>ConfigurationManager.AppSettings</code> lines cease to function correctly. They always return null values per each key even if I manually change the values in the re-created config file.</p>
  <p>This leads me to think there is some underlying way config files are matched up with their exe files?</p>
  <h2>How do I get my application to recognize a dynamically generated app.config file?</h2>
  <p><em>Edit</em></p>
  <p>I added the code I am using to grab the values from the config file below, as well as the current config file I have that has been created by my app.</p>
  <pre><code> _targetDatabaseName = ConfigurationManager.AppSettings[&quot;targetDatabaseName&quot;];
          _applicationServer = ConfigurationManager.AppSettings[&quot;applicationServer&quot;];
          _sqlServer = ConfigurationManager.AppSettings[&quot;sqlServer&quot;];
          _applicationServerPort = ConfigurationManager.AppSettings[&quot;applicationServerPort&quot;];
  
          var emptyKeys = new List&lt;string&gt;();
  
          if (string.IsNullOrEmpty(_targetDatabaseName))
              emptyKeys.Add(&quot;targetDatabaseName&quot;);
  
          if(string.IsNullOrEmpty(_applicationServer))
              emptyKeys.Add(&quot;applicationServer&quot;);
  
          if(string.IsNullOrEmpty(_sqlServer))
              emptyKeys.Add(&quot;sqlServer&quot;);
  
          if(string.IsNullOrEmpty(_applicationServerPort))
              emptyKeys.Add(&quot;applicationServerPort&quot;);
  
          if (emptyKeys.Count &gt; 0)
          {
              Console.WriteLine(&quot;You are missing one of the following required keys &quot;
              + string.Join(&quot;,&quot;, emptyKeys) +&quot;. Press&quot;
                  + &quot; a key to exit the application.&quot;);
  
              Console.ReadKey();
              Environment.Exit(0);
          }  
  </code></pre>
  <p>Here is the current config file as well with real values</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-16&quot;?&gt;
  &lt;configuration&gt;
    &lt;startup&gt;
      &lt;supportedRuntime version=&quot;v4.0&quot; sku=&quot;.NETFramework,Version=v4.5&quot; /&gt;
    &lt;/startup&gt;
    &lt;appSettings&gt;
      &lt;add key=&quot;targetDatabaseName&quot; value=&quot;&quot; /&gt;
      &lt;add key=&quot;applicationServer&quot; value=&quot;&quot; /&gt;
      &lt;add key=&quot;sqlServer&quot; value=&quot;&quot; /&gt;
      &lt;add key=&quot;applicationServerPort&quot; value=&quot;&quot; /&gt;
      &lt;add key=&quot;customSearchPath&quot; value=&quot;&quot; /&gt;
    &lt;/appSettings&gt;
  &lt;/configuration&gt;
  </code></pre>