Assembly: System.Configuration (in system.configuration.dll)
The ConfigurationManager class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated.
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 ConfigurationManager 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 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 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.
Read permission on the configuration file at the current configuration hierarchy level.
Read permissions on all the parent configuration files.
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 the appSettings configuration section. If the section does not exist, it is created and added to the configuration file.
The example works with the elements illustrated in the following configuration file, which is generated the first time that you run the example.
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="NewKey0" value="Monday, January 23, 2006 2:56:14 PM" /> <add key="NewKey1" value="Monday, January 23, 2006 3:15:18 PM" /> <add key="NewKey2" value="Monday, January 23, 2006 3:16:29 PM" /> </appSettings> </configuration>
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.