如何从AppSettings中获取键值.配置文件

本文关键字:获取 键值 配置文件 AppSettings | 更新日期: 2023-09-27 18:03:05

我试图在应用程序设置中设置我的键值。配置文件,但似乎不工作。

这就是我写的。代码是从MDI文件的构造函数调用的,它只返回空值。有人知道为什么吗?

     var getValue = ConfigurationSettings.AppSettings["ShowQueryTextbox"];

我也尝试了ConfigurationManager。AppSettings。

我的AppSettings Code如下:

<configuration>
  <appSettings>
    <add key="ShowQueryTextbox" value="true"/>
  </appSettings>
</configuration>

如何从AppSettings中获取键值.配置文件

ConfigurationSettings。AppSettings已经过时了,试试

ConfigurationManager.AppSettings["ShowQueryTextbox"];

记住:

ConfigurationManager.AppSettings["MyKey"];

需要添加对System的引用。在项目中配置

在将App.Config文件重命名为AppSettings.Config时出现问题。谢谢你的指导和帮助。

ConfigurationManager仍然是最新的- 2017年。

顺便说一句,如果您只是想将appsettings配置值从字符串转换为bool,那么使用Convert.ToBoolean

    if (Convert.ToBoolean(ConfigurationManager.AppSettings["EnableLoggingInfo"]))
    {
        log.Info(message);
    }

在你的appsettings配置(web.config)

<appSettings>
    <add key="EnableLoggingInfo" value="true" />
  </appSettings>

我可以这样做:

System.Configuration.ConfigurationManager.AppSettings.Get("KEY").ToString();

假设您已将其添加到所需的配置文件中,您是否可以检查您试图访问的密钥的大小写敏感,因此如果您键入了不同的大小写,它将不会返回预期的值

如果应用程序设置在错误的配置文件中,也可能出现此错误-例如,在WCF应用程序中,它应该是托管项目中的应用程序

检查Properties.Settings.Default.ShowQueryTextbox