Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

appsettings

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>
like image 765
NewBie Avatar asked Jul 21 '11 11:07

NewBie


People also ask

How read data from web config file in C#?

Read Key value from WebConfig using C# string constring = ConfigurationManager. ConnectionStrings["ABCD"]. ConnectionString; using (SqlConnection con = new SqlConnection(constring)) { //do database operations like read table data or save data. }

How do I access ConfigurationManager AppSettings?

To access these values, there is one static class named ConfigurationManager, which has one getter property named AppSettings. We can just pass the key inside the AppSettings and get the desired value from AppSettings section, as shown below. When we implement the code given above, we get the output, as shown below.


5 Answers

ConfigurationSettings.AppSettings are obsolete, try

ConfigurationManager.AppSettings["ShowQueryTextbox"];
like image 113
Andreas Ågren Avatar answered Oct 03 '22 18:10

Andreas Ågren


Remember that to use:

ConfigurationManager.AppSettings["MyKey"];

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

like image 33
fabriciorissetto Avatar answered Oct 03 '22 18:10

fabriciorissetto


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

like image 31
NewBie Avatar answered Oct 03 '22 17:10

NewBie


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>
like image 34
Marvin Glenn Lacuna Avatar answered Oct 03 '22 17:10

Marvin Glenn Lacuna


I am able to get like this:

System.Configuration.ConfigurationManager.AppSettings.Get("KEY").ToString();
like image 41
Suresh Kamrushi Avatar answered Oct 03 '22 19:10

Suresh Kamrushi