如何检查应用程序设置是否可用?
即。的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
}
答案 0 :(得分:198)
MSDN: Configuration Manager.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
}
答案 1 :(得分:67)
if (ConfigurationManager.AppSettings.AllKeys.Contains("myKey"))
{
// Key exists
}
else
{
// Key doesn't exist
}
答案 2 :(得分:8)
通过泛型和LINQ安全返回默认值。
public T ReadAppSetting<T>(string searchKey, T defaultValue, StringComparison compare = StringComparison.Ordinal)
{
if (ConfigurationManager.AppSettings.AllKeys.Any(key => string.Compare(key, searchKey, compare) == 0)) {
try
{ // see if it can be converted.
var converter = TypeDescriptor.GetConverter(typeof(T));
if (converter != null) defaultValue = (T)converter.ConvertFromString(ConfigurationManager.AppSettings.GetValues(searchKey).First());
}
catch { } // nothing to do just return the defaultValue
}
return defaultValue;
}
使用如下:
string LogFileName = ReadAppSetting("LogFile","LogFile");
double DefaultWidth = ReadAppSetting("Width",1280.0);
double DefaultHeight = ReadAppSetting("Height",1024.0);
Color DefaultColor = ReadAppSetting("Color",Colors.Black);
答案 3 :(得分:2)
如果您要查找的密钥不在配置文件中,您将无法将其转换为带有.ToString()的字符串,因为该值将为null并且您将获得“对象”引用未设置为对象的实例“错误。在尝试获取字符串表示之前,最好首先查看该值是否存在。
if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["myKey"]))
{
String myKey = ConfigurationManager.AppSettings["myKey"].ToString();
}
或者,正如Code Monkey建议:
if (ConfigurationSettings.AppSettings["myKey"] != null)
{
// Now do your magic..
}
答案 4 :(得分:2)
如果您知道密钥类型尝试解析它们,则上层选项可以灵活应对所有方式
bool.TryParse(ConfigurationManager.AppSettings["myKey"], out myvariable);
答案 5 :(得分:2)
我认为LINQ表达式可能是最好的:
const string MyKey = "myKey"
if (ConfigurationManager.AppSettings.AllKeys.Any(key => key == MyKey))
{
// Key exists
}
答案 6 :(得分:2)
var isAlaCarte = ConfigurationManager.AppSettings.AllKeys.Contains(&#34; IsALaCarte&#34;)&amp;&amp; bool.Parse(ConfigurationManager.AppSettings.Get(&#34; IsALaCarte&#34));
答案 7 :(得分:1)
我喜欢codebender's answer,但需要它才能在C ++ / CLI中工作。这就是我最后得到的。没有LINQ用法,但是可以。
generic <typename T> T MyClass::ReadAppSetting(String^ searchKey, T defaultValue) {
for each (String^ setting in ConfigurationManager::AppSettings->AllKeys) {
if (setting->Equals(searchKey)) { // if the key is in the app.config
try { // see if it can be converted
auto converter = TypeDescriptor::GetConverter((Type^)(T::typeid));
if (converter != nullptr) { return (T)converter->ConvertFromString(ConfigurationManager::AppSettings[searchKey]); }
} catch (Exception^ ex) {} // nothing to do
}
}
return defaultValue;
}
答案 8 :(得分:0)
在TryParse中使用新的c#语法对我来说效果很好:
// TimeOut
if (int.TryParse(ConfigurationManager.AppSettings["timeOut"], out int timeOut))
{
this.timeOut = timeOut;
}