104

How can I add and read the value from web.config file?

Vikrant
  • 4,920
  • 17
  • 48
  • 72
Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175

6 Answers6

161

Given the following web.config:

    <appSettings>
         <add key="ClientId" value="127605460617602"/>
         <add key="RedirectUrl" value="http://localhost:49548/Redirect.aspx"/>
    </appSettings>

Example usage:

using System.Configuration;

string clientId = ConfigurationManager.AppSettings["ClientId"];
string redirectUrl = ConfigurationManager.AppSettings["RedirectUrl"];
shA.t
  • 16,580
  • 5
  • 54
  • 111
PrateekSaluja
  • 14,680
  • 16
  • 54
  • 74
  • 18
    +1 nice answer. However one note - you don't need to call `ToString` explicitly, as indexers on `AppSettings` return value of type `string` themselves – horgh Nov 07 '13 at 00:36
78

I would suggest you to don't modify web.config from your, because every time when change, it will restart your application.

However you can read web.config using System.Configuration.ConfigurationManager.AppSettings

Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • Thank You Mr.Muhammed , so what you advice me to do to save a variable in a public place that can be changed without restating the web application ? Thanks in Advance – Amira Elsayed Ismail Oct 04 '10 at 11:41
  • 2
    You can store such variables in an encrypted XML file. – vamyip Oct 04 '10 at 12:01
  • 2
    yes, XML file is the better idea. Or you can store it in DB and add in application_start (Global.asax), put it in application variable and use these in application. these variable assign only once in the application and if your application restart, these will assigned again. – Muhammad Akhtar Oct 04 '10 at 12:31
  • 1
    Thank you very much Mr.Vamyip and Mr.Muhammed for your help – Amira Elsayed Ismail Oct 04 '10 at 12:48
16

If you want the basics, you can access the keys via:

string myKey = System.Configuration.ConfigurationManager.AppSettings["myKey"].ToString();
string imageFolder = System.Configuration.ConfigurationManager.AppSettings["imageFolder"].ToString();

To access my web config keys I always make a static class in my application. It means I can access them wherever I require and I'm not using the strings all over my application (if it changes in the web config I'd have to go through all the occurrences changing them). Here's a sample:

using System.Configuration;

public static class AppSettingsGet
{    
    public static string myKey
    {
        get { return ConfigurationManager.AppSettings["myKey"].ToString(); }
    }

    public static string imageFolder
    {
        get { return ConfigurationManager.AppSettings["imageFolder"].ToString(); }
    }

    // I also get my connection string from here
    public static string ConnectionString
    {
       get { return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; }
    }
}
Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
7

Assuming the key is contained inside the <appSettings> node:

ConfigurationSettings.AppSettings["theKey"];

As for "writing" - put simply, dont.

The web.config is not designed for that, if you're going to be changing a value constantly, put it in a static helper class.

RPM1984
  • 72,246
  • 58
  • 225
  • 350
3

Ryan Farley has a great post about this in his blog, including all the reasons why not to write back into web.config files: Writing to Your .NET Application's Config File

CD..
  • 72,281
  • 25
  • 154
  • 163
0

I am siteConfiguration class for calling all my appSetting like this way. I share it if it will help anyone.

add the following code at the "web.config"

<configuration>
   <configSections>
     <!-- some stuff omitted here -->
   </configSections>
   <appSettings>
      <add key="appKeyString" value="abc" />
      <add key="appKeyInt" value="123" />  
   </appSettings>
</configuration>

Now you can define a class for getting all your appSetting value. like this

using System; 
using System.Configuration;
namespace Configuration
{
   public static class SiteConfigurationReader
   {
      public static String appKeyString  //for string type value
      {
         get
         {
            return ConfigurationManager.AppSettings.Get("appKeyString");
         }
      }

      public static Int32 appKeyInt  //to get integer value
      {
         get
         {
            return ConfigurationManager.AppSettings.Get("appKeyInt").ToInteger(true);
         }
      }

      // you can also get the app setting by passing the key
      public static Int32 GetAppSettingsInteger(string keyName)
      {
          try
          {
            return Convert.ToInt32(ConfigurationManager.AppSettings.Get(keyName));
        }
        catch
        {
            return 0;
        }
      }
   }
}

Now add the reference of previous class and to access a key call like bellow

string appKeyStringVal= SiteConfigurationReader.appKeyString;
int appKeyIntVal= SiteConfigurationReader.appKeyInt;
int appKeyStringByPassingKey = SiteConfigurationReader.GetAppSettingsInteger("appKeyInt");
reza.cse08
  • 5,938
  • 48
  • 39