29

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>
NewBie
  • 1,824
  • 8
  • 35
  • 66

8 Answers8

29

ConfigurationSettings.AppSettings are obsolete, try

ConfigurationManager.AppSettings["ShowQueryTextbox"];
Andreas Ågren
  • 3,879
  • 24
  • 33
13

Remember that to use:

ConfigurationManager.AppSettings["MyKey"];

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

fabriciorissetto
  • 9,475
  • 5
  • 65
  • 73
4

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

NewBie
  • 1,824
  • 8
  • 35
  • 66
  • In a test scenario, we don't know ahead of time which key we are loading. We load ALL keys into a dictionary for use. So getting the key your suggested way means we have to know the key name, and we don't have that info at run time. – Su Llewellyn Jan 24 '18 at 18:32
1

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>
Marvin Glenn Lacuna
  • 1,682
  • 1
  • 19
  • 25
1

I am able to get like this:

System.Configuration.ConfigurationManager.AppSettings.Get("KEY").ToString();
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
0

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

0

Check Properties.Settings.Default.ShowQueryTextbox.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • 5
    Please don't give one-line answers! See [what is concidered a good answer](http://stackoverflow.com/help/how-to-answer) on [so]! Thank you! – jkalden Feb 04 '17 at 13:11
0

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.

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
  • It's the same as i copy-pasted it from config file. – NewBie Jul 21 '11 at 11:18
  • Highly unlikely of something like this happening,could you rebuild and try since that would be only file to look when accessing via `ConfigurationManager` – V4Vendetta Jul 21 '11 at 11:24
  • Is there anything to do with this header in my config file? – NewBie Jul 21 '11 at 11:34
  • Not at all that's fine, Can you check in the debug folder whether you are getting these values in the xml file there ? – V4Vendetta Jul 21 '11 at 11:40
  • I dont know which file to refer. – NewBie Jul 21 '11 at 11:48
  • It would be named as appname.exe.config that is the configuration file else you are adding it in the wrong file, Are there multiple ? – V4Vendetta Jul 21 '11 at 11:50
  • I got a desktop as well as web project within the same solution. On creating the desktop project didnt created any config file. I added that manually and wrote the above code in my post. There is a web.config file in my webproject too. I couldnt find the file you referred in my debug folder. – NewBie Jul 21 '11 at 11:59
  • V4Vendetta...Thanks for the help. The issue arise on renaming the App.Config file as AppSettings.Config. – NewBie Jul 21 '11 at 12:31