Export (0) Print
Expand All

ConfigurationManager Class

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

System.Object
  System.Configuration.ConfigurationManager

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

[<AbstractClass>]
[<Sealed>]
type ConfigurationManager =  class end

The ConfigurationManager type exposes the following members.

  NameDescription
Public propertyStatic memberAppSettingsGets the AppSettingsSection data for the current application's default configuration.
Public propertyStatic memberConnectionStringsGets the ConnectionStringsSection data for the current application's default configuration.
Top

  NameDescription
Public methodStatic memberGetSectionRetrieves a specified configuration section for the current application's default configuration.
Public methodStatic memberOpenExeConfiguration(ConfigurationUserLevel)Opens the configuration file for the current application as a Configuration object.
Public methodStatic memberOpenExeConfiguration(String)Opens the specified client configuration file as a Configuration object.
Public methodStatic memberOpenMachineConfigurationOpens the machine configuration file on the current computer as a Configuration object.
Public methodStatic memberOpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel)Opens the specified client configuration file as a Configuration object that uses the specified file mapping and user level.
Public methodStatic memberOpenMappedExeConfiguration(ExeConfigurationFileMap, ConfigurationUserLevel, Boolean)Opens the specified client configuration file as a Configuration object that uses the specified file mapping, user level, and preload option.
Public methodStatic memberOpenMappedMachineConfigurationOpens the machine configuration file as a Configuration object that uses the specified file mapping.
Public methodStatic memberRefreshSectionRefreshes the named section so the next time that it is retrieved it will be re-read from disk.
Top

The ConfigurationManager class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated. For web applications, use the WebConfigurationManager class.

To use the ConfigurationManager class, your project must reference the System.Configuration assembly. By default, some project templates, like Console Application, do not reference this assembly so you must manually reference it.

NoteNote

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 ConfigurationManager class includes members that enable you to perform the following tasks:

Notes to Implementers

The Configuration class enables 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, 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 first example shows a simple console application that reads application settings, adds a new setting, and updates an existing setting.

No code example is currently available or this language may not be supported.

The previous example assumes your project has an App.config file as shown below.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <appSettings>
    <add key="Setting1" value="May 5, 2014"/>
    <add key="Setting2" value="May 6, 2014"/>
  </appSettings>
</configuration>

The following example shows how to use a connection string to read data from a database.

No code example is currently available or this language may not be supported.

The previous example assumes your project has an App.config as shown below.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <connectionStrings>
      <add name="WingtipToys" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=WingtipToys;Integrated Security=True;Pooling=False" />
    </connectionStrings>
</configuration>

A Visual Studio Web site project with source code is available to accompany this topic: Download.

The following example shows how to use the ConfigurationManager 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.

    NoteNote

    When you create a project, make sure to add a reference to the System.Configuration assembly. This assembly contains the ConfigurationManager class.

No code example is currently available or this language may not be supported.

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=1.0.0.0, 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>

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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