appSettings キーが存在するかどうかを確認するにはどうすればいいですか? 質問する

appSettings キーが存在するかどうかを確認するにはどうすればいいですか? 質問する

アプリケーション設定が利用可能かどうかを確認するにはどうすればよいですか?

例えばapp.config

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

そしてコードファイルでは

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

ベストアンサー1

MSDN: 構成マネージャー.AppSettings

if (ConfigurationManager.AppSettings[name] != null)
{
// Now do your magic..
}

または

string s = ConfigurationManager.AppSettings["myKey"];
if (!String.IsNullOrEmpty(s))
{
    // Key exists
}
else
{
    // Key doesn't exist
}

おすすめ記事