ConfigurationManager Class in ASP.NET 2.0 - Managing App Settings

Last Updated: Nov 04, 2025
5 min read
Legacy Archive
Legacy Guidance: This article preserves historical web development content. For modern .NET 8+ best practices, visit our Tutorials section.

Some values and strings are used across all pages of a website. It's a tedious process to code those values and strings in each page of the website. For example, consider a connection string. If you're developing a database-driven website, that connection string will be used in many pages.

Let's say you've hard-coded the connection string in all your pages—around 30 pages. Now if the value of the connection string changes, you've got a headache on your hands. You'll need to change all the values of the connection string in all 30 pages. This can't be done easily, and if you need to change the string again after about a month, it's going to be a tedious task.

The Central Repository Solution

In such scenarios, it's a best practice to store those values in a central repository from where you can retrieve and use them in all your pages. For example, if you store the connection string in a central place like a web.config file, you can retrieve those connection strings from the web.config file and use them throughout your application.

Previous to ASP.NET 2.0, connection strings were stored in the web.config file using the <appSettings> element. The connection string was stored as a key-value pair, and you'd retrieve it using ConfigurationSettings.AppSettings("cString").

web.config (Pre-ASP.NET 2.0)
<configuration>
  <appSettings>
    <add key="cString" value="connection_string_value_here" />
  </appSettings>

  <system.web>
    ...
  </system.web>
</configuration>

Introducing ConfigurationManager Class

ASP.NET 2.0 provides a special class for retrieving those customized string values. This class is called the ConfigurationManager class. Using this class, you'll be retrieving values with cleaner, more intuitive syntax.

Instead of using the <appSettings> section, you can also add your own sections in the web.config file to organize all your string values that you'll be using in your application.

RetrievingAppSettings.cs
using System.Configuration;

// Retrieve a value from appSettings section
string connectionString = ConfigurationManager.AppSettings["cString"];

// Use it in your application
Console.WriteLine($"Connection: {connectionString}");

Working with ConnectionStrings Section

To access the ConnectionStrings section of the web.config file, you can use the dedicated ConnectionStrings property. This is the preferred way to store database connection strings in ASP.NET 2.0 and later.

web.config (ASP.NET 2.0+)
<configuration>
  <connectionStrings>
    <add name="prodCS" 
         connectionString="Data Source=(local);Initial Catalog=Products;Integrated Security=True"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    ...
  </system.web>
</configuration>
AccessingConnectionString.cs
using System.Configuration;
using System.Data.SqlClient;

// Retrieve the connection string
string connString = ConfigurationManager.ConnectionStrings["prodCS"].ConnectionString;

// Use it with SqlConnection
using (SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();
    // Your database operations here
}

Other ConfigurationManager Methods

The ConfigurationManager class provides several other useful methods for working with configuration files:

  • GetSection: Retrieves a particular section from the configuration file in the current directory
  • GetWebAppSection: Gets a section from the current web application's root directory
  • RefreshSection: Refreshes a named section to reload values from disk
  • OpenMachineConfiguration: Opens the machine.config file
  • OpenMappedMachineConfiguration: Opens any specified machine.config file
  • OpenExeConfiguration: Opens a client's configuration file
  • OpenMappedExeConfiguration: Opens a mapped executable configuration file
  • OpenWebConfiguration: Opens the current web application's configuration files
  • OpenMappedWebConfiguration: Opens a mapped web configuration file
UsingConfigMethods.cs
using System.Configuration;

// Get a custom section from current directory
var section = ConfigurationManager.GetSection("prodSection");

// Get a section from root directory
var webSection = ConfigurationManager.GetWebAppSection("prodSection");

// Open web configuration for editing
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
var connStrings = config.ConnectionStrings.ConnectionStrings;

// Modify and save configuration
connStrings.Add(new ConnectionStringSettings("newCS", 
    "Server=localhost;Database=mydb;Trusted_Connection=true;"));
config.Save();

Summary

The ConfigurationManager class is used to retrieve, edit, and update configuration file values programmatically. This class provides a new and improved way of managing application settings in ASP.NET 2.0 compared to the older ConfigurationSettings class.

By centralizing your configuration values, you'll make your application easier to maintain and modify. Whether you're working with connection strings, app settings, or custom configuration sections, ConfigurationManager gives you the tools you need to manage them effectively.

FAQ

Why should I store connection strings in web.config instead of hard-coding them?

Storing connection strings centrally in web.config means you only need to update them in one place. If you hard-code them across 30 pages and need to change the database server, you'll have to update all 30 files, which is tedious and error-prone.

What's the difference between AppSettings and ConnectionStrings sections?

ConnectionStrings is specifically designed for database connection strings with built-in properties like ConnectionString and ProviderName. AppSettings is for general key-value pairs like API keys, settings, and other configuration values.

Can I read configuration files from different locations?

Yes, ConfigurationManager provides methods like OpenWebConfiguration for web apps, OpenExeConfiguration for client apps, and OpenMachineConfiguration for machine.config. You can read configuration from the current directory or root directory.

What replaced ConfigurationSettings.AppSettings in ASP.NET 2.0?

ConfigurationManager.AppSettings replaced the older ConfigurationSettings.AppSettings. The new class provides better functionality and supports the dedicated ConnectionStrings section, making configuration management more organized.

Back to Articles