在 app.config 文件中,如何判断是否包含特定的键?C#程序集>应用程序设置>如何检查是否存在一个?
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
}
试试这个:
if(ConfigurationManager.AppSettings["yourkey"] != null)
{
// that key exists..... do something with it
}
Contains
方法。 - user195488