Ramani Sandeep's Blog

DotNetting – Fast , Easy Way of Developing Applications

Programming the web.config File Using C#

Posted by ramanisandeep on April 7, 2009

What Is web.config?

The web.config file is the application’s configuration file. It is typically used to configure an ASP.NET Web application and define the configuration settings for the Web application. It typically contains the application-wide settings, such as database connection string, culture settings, authentication, and authorization information, etc. In ASP.NET 1.x, much effort was required to manipulate the web.config file programmatically. With ASP.NET 2.0 however, this can be done quite easily and efficiently. The following section discusses how this can be achieved.

The Configuration API in ASP.NET 2.0

The configuration API of ASP.NET 2.0 adds a great level of flexibility in that it allows us to add or edit a configuration file seamlessly in ASP.NET. The WebConfigurationManager class in the System.Web.Configuration namespace has the OpenWebConfiguration method that can be used to open the configuration file of the application as a Configuration object for reading from or writing to the configuration file. The virtual path to the configuration file is specified to this method as a parameter.

The following code snippet displays all the keys of the appSettings section of the web.config file:

Configuration configuration = WebConfigurationManager.OpenWebConfiguration(“~”);
AppSettingsSection appSettingsSection =
(AppSettingsSection)configuration.GetSection(“appSettings”);
if (appSettingsSection != null)
{
foreach (string key in appSettingsSection.Settings.AllKeys)
{
Response.Write(key);
}
}


The following method can be used to modify a specific key — value pair of the web.config file — programmatically using C#:

public void Modify(string key, string value)
{
Configuration configuration = WebConfigurationManager.OpenWebConfiguration(“~”);
AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection(“appSettings”);
if (appSettingsSection != null)
{
appSettingsSection.Settings[key].Value = value;
config.Save();
}
}

The following method can be used to delete a specific key in the web.config file programmatically using C#:

public void Remove(string key)
{
Configuration configuration = WebConfigurationManager.OpenWebConfiguration(“~”);
AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection(“appSettings”);
if (appSettingsSection != null)
{
appSettingsSection.Settings.Remove(key);
config.Save();
}
}

Conclusion

Even if modifying a web.config file programmatically can be a handy solution in some situations, it is not recommended to do so frequently in a Web application, as any change in the web.config file will restart the Web server and refresh the cache entries.

4 Responses to “Programming the web.config File Using C#”

  1. Binto said

    Hey Ramani,
    Great posting.Keep ur good work.
    I want to get a favor from you.
    I have developed a web application.I have hosted my application in two different location.In this two locations I have separate server and 50-100 users using my application through LAN.My application and DB are same in name but having different data.My requirement
    is : I want to access the two DB server located in two different locations through the same application hosted in internet.Say ADB is the DB server name located in place A,BDB is the DB server name located in place B & APP1 is the application in internet.What I am planning to do is : I will develop a page for changing the DB locations in the application hosted in internet.Can I change the Web config file for changing the DB server name.Is it a good solution.Can you please suggest some solutions

    Thanks in advance
    Binto Thomas

  2. Binto said

    Hei Ramani

    I am using in web.config.
    How can I change each and every key and value in appSettings

    Please help me

    Regards,
    Binto Thomas

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>