C# 如何确定appconfig包含特定密钥

C# 如何确定appconfig包含特定密钥,c#,.net,C#,.net,可能重复: 在app.config中,我如何知道它是否包含特定密钥 var specificValue = ConfigurationManager.AppSettings["specificKey"]; if (!string.IsNullOrEmpty(specificValue)) { // Use the value } 但是,如果您只想查看状态,您还可以: if (ConfigurationManager.AppSettings.AllKeys.Contains("spec

可能重复:

在app.config中,我如何知道它是否包含特定密钥

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
}

您的第二个选项是错误的-ConfigurationManager.AppSettings.AllKeys.ContainsSpecificEy没有这样的选项method@briler:有。请看下面的示例代码:它返回一个字符串数组,然后您可以在字符串数组上使用Contains。我不确定您的第一个示例是否正确。若你们在配置文件中并没有像specificKey这样的索引键,你们会得到异常,不是吗?