Provides access to configuration files for client applications. This class cannot be inherited.
Assembly: System.Configuration (in System.Configuration.dll)
The class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated.
The name and location of the application configuration file depend on the application's host. For more information, see Application Configuration Files.
You can use the built-in System.Configuration types or derive from them to handle configuration information. By using these types, you can work directly with configuration information and you can extend configuration files to include custom information.
The class includes members that enable 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 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 create the configuration data every time that 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 that uses the attribute-based model, see ConfigurationElement.
The Configuration class enables programmatic access for editing configuration files. You use one of the Open methods provided by . 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.
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, we recommend that you use 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 example shows how to use the class in a console application. The code shows the following:
How to access the appSettings configuration section. If the section does not exist, it is created and added to the configuration file. The example creates the application configuration file if one does not exist.
How to access the Machine.config file to obtain information such as the default connection string, if it is defined, and the sections that are currently configured.
When you create a project, make sure to add a reference to the System.Configuration assembly. This assembly contains the class.
A Visual Studio project with source code is available to accompany this topic: Download.
The example works with elements that are similar to the ones illustrated in the following configuration file. These elements are generated the first time that you run the example.
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="consoleSection" type="Samples.Aspnet.ConsoleSection, ConfigurationManager_CS, Version=184.108.40.206, Culture=neutral, PublicKeyToken=null" /> </configSections> <appSettings> <add key="NewKey0" value="Monday, March 30, 2009 1:36:33 PM" /> <add key="NewKey1" value="Monday, March 30, 2009 1:36:40 PM" /> </appSettings> <consoleSection> <consoleElement background="Black" foreground="White" /> </consoleSection> </configuration>
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.