title: Reading custom configuration section's key values in C#
body: <p>I need to read key values from custom sections in app/web.config.</p>
  
  <p>I went through </p>
  
  <p><a href="https://stackoverflow.com/questions/4595288/reading-a-key-from-the-web-config-using-configurationmanager">Reading a key from the Web.Config using ConfigurationManager</a></p>
  
  <p>and</p>
  
  <p><a href="https://stackoverflow.com/questions/15254566/how-can-i-retrieve-list-of-custom-configuration-sections-in-the-config-file-usi">How can I retrieve list of custom configuration sections in the .config file using C#?</a></p>
  
  <p>However, they do not specify how to read a custom section when we need to explicitly specify the path to the configuration file (in my case, the configuration file is not in it's default location)</p>
  
  <p>Example of my web.config file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
    &lt;MyCustomTag&gt; 
      &lt;add key="key1" value="value1" /&gt;
      &lt;add key="key2" value="value2" /&gt;
    &lt;/MyCustomTag&gt;
  &lt;system.web&gt;
    &lt;compilation related data /&gt;
   &lt;/system.web&gt; 
  &lt;/configuration&gt;
  </code></pre>
  
  <p>in which i need to read key value pairs inside MyCustomTag.</p>
  
  <p>When i try (configFilePath is the path to my configuration file):-</p>
  
  <pre><code>var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
  
  var config =
            ConfigurationManager.OpenMappedExeConfiguration(
              configFileMap, ConfigurationUserLevel.None);
  
          ConfigurationSection section = config.GetSection(sectionName);
  
          return section[keyName].Value;
  </code></pre>
  
  <p>I get a error stating "Cannot access protected internal indexer 'this' here" at section[keyName]</p>