开发者

How to get the key value from the AppSettings.Config file?

开发者 https://www.devze.com 2023-03-22 11:50 出处:网络
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

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 = Configurat开发者_Python百科ionSettings.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>


ConfigurationSettings.AppSettings are obsolete, try

ConfigurationManager.AppSettings["ShowQueryTextbox"];


Remember that to use:

ConfigurationManager.AppSettings["MyKey"];

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


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


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>


I am able to get like this:

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


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.


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


Check Properties.Settings.Default.ShowQueryTextbox.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号