如何从AppSettings.Config文件中获取键值?

发布于 2024-11-25 10:26:53 字数 459 浏览 2 评论 0原文

我试图在 appsettings.Config 文件中设置我的键值,但似乎不起作用。

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

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

我还尝试使用 ConfigurationManager.AppSettings 。那也不起作用。

我的AppSettings代码如下。

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

I'm trying to get my key value set in the appsettings.Config file but seems not working.

This is what i wrote for that. The code is called from the constructor of an MDI file and its returning only null value. Anybody know why?

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

I also tried with ConfigurationManager.AppSettings . That too didnt work.

My AppSettings Code is as follows.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

Spring初心 2024-12-02 10:26:53

ConfigurationSettings.AppSettings 已过时,请尝试

ConfigurationManager.AppSettings["ShowQueryTextbox"];

ConfigurationSettings.AppSettings are obsolete, try

ConfigurationManager.AppSettings["ShowQueryTextbox"];
谜泪 2024-12-02 10:26:53

请记住要使用:

ConfigurationManager.AppSettings["MyKey"];

您需要将对 System.Configuration 的引用添加到您的项目中。

Remember that to use:

ConfigurationManager.AppSettings["MyKey"];

You need to add reference to System.Configuration to your project.

故事与诗 2024-12-02 10:26:53

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

The issue arise on renaming the App.Config file as AppSettings.Config. Thanks for all the guidances and help.

救赎№ 2024-12-02 10:26:53

ConfigurationManager 仍然是最新的 - 2017 年。

顺便说一句,如果您只想将 appsettings 配置值从字符串转换为布尔值,请

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

在 appsettings 中 使用 Convert.ToBoolean配置(web.config)

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

  </appSettings>

The ConfigurationManager is still up to date - Year 2017.

Btw, if you simply want to convert the appsettings configuration value from string to bool, then use Convert.ToBoolean

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

In your appsettings configuration (web.config)

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

  </appSettings>
九八野马 2024-12-02 10:26:53

我能够得到这样的:

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

I am able to get like this:

System.Configuration.ConfigurationManager.AppSettings.Get("KEY").ToString();
瞄了个咪的 2024-12-02 10:26:53

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

Assuming you have added it to the required config file, Can you check the case of the key you are trying to access it's case sensitive so if you have keyed in a different case, it won't be returning the expected value.

黯淡〆 2024-12-02 10:26:53

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

This error can also arise if you have the appsettings in the wrong configuration file - example in a WCF application it should be the one in the hosting project

一个人练习一个人 2024-12-02 10:26:53

检查Properties.Settings.Default.ShowQueryTextbox。

Check Properties.Settings.Default.ShowQueryTextbox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文