How to check if an appSettings key exists?

2019-01-21 11:39发布

How do I check to see if an Application Setting is available?

i.e. app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key ="someKey" value="someValue"/>
  </appSettings>
</configuration>

and in the codefile

if (ConfigurationManager.AppSettings.ContainsKey("someKey"))
{
  // Do Something
}else{
  // Do Something Else
}

7条回答
趁早两清
2楼-- · 2019-01-21 12:43

Upper options gives flexible to all manner, if you know key type try parsing them bool.TryParse(ConfigurationManager.AppSettings["myKey"], out myvariable);

查看更多
登录 后发表回答