C# .NET Framework 4.5: 如何判断 KeyValueConfigurationCollection 中是否存在某个 Key
使用以下代码可以判断是否存在某个 Key:
if (configurationCollection.AllKeys.Contains('keyName'))
{
// key exists
}
else
{
// key does not exist
}
其中,configurationCollection 是 KeyValueConfigurationCollection 类型的对象,keyName 是要判断的 Key 的名称。AllKeys 属性返回一个字符串数组,包含配置文件中所有的 Key。通过判断该数组中是否包含指定的 Key 名称,来判断该 Key 是否存在。
原文地址: https://www.cveoy.top/t/topic/lU1I 著作权归作者所有。请勿转载和采集!