How to read appSettings section in the web.config file?

realn picture realn · Nov 16, 2011 · Viewed 122.4k times · Source

My XML looks like this and the filename is web.config

<?xml version="1.0"?>
<configuration>
  <appSettings>   
    <add key="configFile" value="IIS.config"/>
    <add key="RialtoDomain" value="ASNC_AUDITORS"/>    
  </appSettings>
  <system.serviceModel>
    ....
  </system.serviceModel>
</configuration>

In the code when I read like this

String path = ConfigurationSettings.AppSettings["configFile"];

I am getting a null value. No exception is thrown. Is this the right way to do it?

Answer

marc_s picture marc_s · Nov 16, 2011

Since you're accessing a web.config you should probably use

using System.Web.Configuration;

WebConfigurationManager.AppSettings["configFile"]