title: How to read values from App.config in .Net 4.0 using configurationManager?
body: <p>I am creating a windows service in .Net 4.0 and testing some functions of said service with a windows forms client by referencing the service project.</p>
  
  <p>The service project has an App.config file and that file looks like this: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
    &lt;configuration&gt;
       &lt;connectionStrings&gt;
         &lt;clear /&gt;
         &lt;add name="myLocalMySQLDBUsername" connectionString="username"/&gt;
       &lt;/connectionStrings&gt;
    &lt;/configuration&gt;
  </code></pre>
  
  <p>When a function belonging to the service calls:</p>
  
  <ul>
  <li>ConfigurationManager.ConnectionStrings("myLocalMySQLDBUsername").ConnectionString </li>
  </ul>
  
  <p>a null reference error is thrown because my connection string is not loaded.  The only connectionStrings that are loaded are from the machine.config file located in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config\machine.config</p>
  
  <p>If I create an application scope setting for the service, I can get that setting by using the My.Settings.setting -> so it's not like the App.config file is not being read.</p>
  
  <p><strong>My question is: why are my connectionStrings not being loaded from the App.config file?</strong></p>
  
  <p>Thank you for your help.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Also, at this point, even a work around would be appreciated; the only reason for using app.config is to be able to encrypt the contents using the DpapiProtectedConfigurationProvider (the contents will have some username/password values for service and database connections).</p>
  
  <p>I tried creating an AppSettings section manually in the app.config but those settings were also not read by the configurationManager (count = 0).</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>Per a suggestion, I tried to manually open the app.config file like so:</p>
  
  <pre><code>Dim exePath As String = System.IO.Path.Combine(Environment.CurrentDirectory, "ServiceName.exe")
  
  Dim myConfig As Configuration = ConfigurationManager.OpenExeConfiguration(exePath)
  </code></pre>
  
  <p>So here is the weird part, when I look inside, path is correct (points to my app.config) but the connectionStrings are still being loaded from the machine.config file (my connectionStrings are not loaded)!! ARGH</p>
  
  <p><strong>UPDATE 3:</strong></p>
  
  <p>Okay, so, I figured it out.  When referencing a project(parent) from another project(child), the child's app.config is used even if the parent's classes are being used.  Thus, I can get the connectionStrings to show up if I copy them over to the child's app.config.  When trying to open it manually, my currentDirectory was of the child, not the parent (strange how it did not throw an exception - it wouldn't have been able to find the config file ... it just silently used the machine.config ... oh well).  </p>
  
  <p><strong>Thanks all for the help!</strong></p>