可能重复:
C# assembly > app settings > how to check if one exists?
在app.config中,我怎么知道它是否包含特定的密钥?
答案 0 :(得分:23)
var specificValue = ConfigurationManager.AppSettings["specificKey"];
if (!string.IsNullOrEmpty(specificValue))
{
// Use the value
}
但如果您只想检查身份,也可以:
if (ConfigurationManager.AppSettings.AllKeys.Contains("specificKey"))
{
// the config file contains the specific key
}
答案 1 :(得分:4)
试试这个:
if(ConfigurationManager.AppSettings["yourkey"] != null)
{
// that key exists..... do something with it
}