Reading A Key From the Web.Config

In this article, I will explain how we can read the configuration setting from the Web.Config or App.Config file. To read the configuration setting we need to add System.Configuration assembly reference.

 

There are a few ways to read configuration settings.

  1. To read a value from the appSettings section

public void Read()
{
  var author = ConfigurationManager.AppSettings["Author"];
}

2. Read the entire section from the Web.Config or App.Config

public void Read()
{
    Dictionary<string, string> settings = new Dictionary<string, string>();
    var authorSettings = ConfigurationManager.GetSection("AuthorSetting") as NameValueCollection;
    if (authorSettings.Count > 0)
    {
        foreach (var key in authorSettings.AllKeys)
        {
            settings.Add(key, authorSettings[key]);
        }
    }
}

1 Comment

  1. Samrin Iqbal

    How can I read “privatePath” from wed.config file ?

    0
    0
    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories