ConfigurationManager Class

Note: This class is new in the .NET Framework version 2.0.

Provides access to configuration files for client applications. This class cannot be inherited.

Namespace: System.Configuration
Assembly: System.Configuration (in system.configuration.dll)

Public NotInheritable Class ConfigurationManager
The members of a static class are accessed directly without an instance of the class.
public final class ConfigurationManager
public final class ConfigurationManager

The ConfigurationManager class allows you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated.

Client applications run on the client computer, while Web applications run on the server computer that the client is connected to.

You can use the built-in System.Configuration types or derive from them to handle configuration information. Using these types, you can work directly with configuration information and you can extend configuration files to include custom information.

The ConfigurationManager class includes members that allow you to perform the following tasks.

  • Read a section from a configuration file. To access configuration information, call the GetSection method. For some sections such as appSettings and connectionStrings, use the AppSettings and ConnectionStrings classes. These members perform read-only operations, use a single cached instance of the configuration, and are multithread aware.

  • Read and write configuration files as a whole. Your application can read and write configuration settings at any level, for itself or for other applications or computers, locally or remotely. Use one of the methods provided by the ConfigurationManager class to open a configuration file such as SampleApp.exe.config. These methods return a Configuration object that in turn exposes methods and properties you can use to work with the associated configuration files. The methods perform read or write operations and recreate the configuration data every time a file is written.

  • Support configuration tasks. The following types are used to support various configuration tasks:

    In addition to working with existing configuration information, you can create and work with custom configuration elements by extending the built-in configuration types such as the ConfigurationElement, ConfigurationElementCollection, ConfigurationProperty, and ConfigurationSection classes. For an example of how to extend a built-in configuration type programmatically, see ConfigurationSection. For an example of how to extend a built-in configuration type using the attribute-based model, see ConfigurationElement.

Notes to Implementers The Configuration class allows programmatic access for editing configuration files. You use one of the Open methods provided by ConfigurationManager. These methods return a Configuration object, which in turn provides the required methods and properties to handle the underlying configuration files. You can access these files for reading or writing. To read the configuration files, use GetSection or GetSectionGroup to read configuration information. The user or process that reads must have the following permissions:

  • Read permission on the configuration file at the current configuration hierarchy level.

  • Read permissions on all the parent configuration files.

If your application needs read-only access to its own configuration, it is recommended that you use of the GetSection method. This method provides access to the cached configuration values for the current application, which has better performance than the Configuration class. To write to the configuration files, use one of the Save methods. The user or process that writes must have the following permissions:
  • Write permission on the configuration file and directory at the current configuration hierarchy level.

  • Read permissions on all the configuration files.

The following code example shows how to use the ConfigurationManager class to access a custom configuration section. If the section does not exist, it is created and added to the configuration file. The custom section class used in this example is implemented in the example contained in the ConfigurationPropertyAttribute class.

 ' Create a custom section.
Shared Sub New()
   ' Get the application configuration file.
     Dim config As System.Configuration.Configuration = _
     ConfigurationManager.OpenExeConfiguration( _
   ' Console.WriteLine(config.FilePath);
   ' If the section does not exiat in the configuration
   ' file, create it and save it to the file.
   If config.Sections(customSectionName) Is Nothing Then
      custSection = New CustomSection()
      config.Sections.Add(customSectionName, custSection)
      custSection = config.GetSection(customSectionName)
      custSection.SectionInformation.ForceSave = True
   End If
End Sub 'New

The example works with the elements illustrated in the following configuration file, which is generated the first time you run the example.


You must set the Version attribute to reflect your version control requirements.

<?xml version="1.0" encoding="utf-8"?>
    <section name="CustomSection" 
         restartOnExternalChanges="true" />
    <add name="ConnStr1" 
      connectionString="LocalSqlServer: data source=;Integrated Security=SSPI;Initial Catalog=aspnetdb"
      providerName="System.Data.SqlClient" />
    <add name="ConnStr2" 
      connectionString="LocalSqlServer: data source=;Integrated Security=SSPI;Initial Catalog=aspnetdb"
      providerName="System.Data.SqlClient" />
    <add key="AppSetting0" 
      value="Monday, February 28, 2005 1:27:59 PM" />
    <add key="AppSetting1" 
      value="Monday, February 28, 2005 1:36:40 PM" />
  <CustomSection fileName="default.txt" maxUsers="1000" 
     maxIdleTime="00:10:00" />


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions