Alex Rivera | Logout

How to determine appconfig contains a specific key

Asked 2010-12-23T10:15:42.743
10

Possible Duplicate:
C# assembly > app settings > how to check if one exists?

In the app.config, How can I know if it contains a specific key?

Edit
Report

1 Answer

23
var specificValue = ConfigurationManager.AppSettings["specificKey"];
if (!string.IsNullOrEmpty(specificValue))
{
    // Use the value
}

but if you only want to check the presence you could also:

if (ConfigurationManager.AppSettings.AllKeys.Contains("specificKey"))
{
    // the config file contains the specific key    
}
answered 2010-12-23T10:17:43.867

Your Answer