AppSettings get value from .config file

Matt picture Matt · May 26, 2012 · Viewed 541.3k times · Source

I'm not able to access values in configuration file.

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var clientsFilePath = config.AppSettings.Settings["ClientsFilePath"].Value; 
// the second line gets a NullReferenceException

.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!-- ... -->
    <add key="ClientsFilePath" value="filepath"/>
    <!-- ... -->
  </appSettings>
</configuration>

Do you have any suggestion what should I do?

Answer

Adam picture Adam · May 26, 2012

This works for me:

string value = System.Configuration.ConfigurationManager.AppSettings[key];