[英]How to add and access key value pairs from appsettings.config file in visual studio code?
[英]How to get the key value from the AppSettings.Config file?
我正在尝试在 appsettings.Config 文件中设置我的键值,但似乎无法正常工作。
这就是我为此写的。 该代码是从 MDI 文件的构造函数中调用的,它只返回 null 值。 有人知道为什么吗?
var getValue = ConfigurationSettings.AppSettings["ShowQueryTextbox"];
我还尝试了 ConfigurationManager.AppSettings。 那也没用。
我的 AppSettings 代码如下。
<configuration>
<appSettings>
<add key="ShowQueryTextbox" value="true"/>
</appSettings>
</configuration>
ConfigurationSettings.AppSettings 已过时,请尝试
ConfigurationManager.AppSettings["ShowQueryTextbox"];
记住要使用:
ConfigurationManager.AppSettings["MyKey"];
您需要在项目中添加对System.Configuration的引用。
将 App.Config 文件重命名为 AppSettings.Config 时会出现问题。 感谢所有的指导和帮助。
我能够得到这样的:
System.Configuration.ConfigurationManager.AppSettings.Get("KEY").ToString();
如果您的配置文件中的 appsettings 错误,也会出现此错误 - 例如在 WCF 应用程序中,它应该是托管项目中的那个
检查Properties.Settings.Default.ShowQueryTextbox
。
ConfigurationManager
仍然是最新的 - 2017 年。
顺便说一句,如果您只是想将 appsettings 配置值从字符串转换为布尔值,请使用Convert.ToBoolean
if (Convert.ToBoolean(ConfigurationManager.AppSettings["EnableLoggingInfo"]))
{
log.Info(message);
}
在您的 appsettings 配置中 (web.config)
<appSettings>
<add key="EnableLoggingInfo" value="true" />
</appSettings>
假设您已将其添加到所需的配置文件中,您能否检查您尝试访问的密钥的大小写是否区分大小写,因此如果您键入了不同的大小写,它将不会返回预期值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.