Search code examples
c#appsettingsconfigurationmanager

How to check if an appSettings key exists?


How do I check to see if an Application Setting is available?

i.e. app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key ="someKey" value="someValue"/>
  </appSettings>
</configuration>

and in the codefile

if (ConfigurationManager.AppSettings.ContainsKey("someKey"))
{
  // Do Something
}else{
  // Do Something Else
}

Solution

  • MSDN: Configuration Manager.AppSettings

    if (ConfigurationManager.AppSettings[name] != null)
    {
    // Now do your magic..
    }
    

    or

    string s = ConfigurationManager.AppSettings["myKey"];
    if (!String.IsNullOrEmpty(s))
    {
        // Key exists
    }
    else
    {
        // Key doesn't exist
    }