Freigeben über


Configuration API: Using System.Configuration.ConfigurationManager

Introduction

This post shows a console application that uses the ConfigurationManager class. The code example extends what is already in the current documentation to demonstrate the use of this class. The example has been built using Microsoft Visual Studio 2008 and the .NET Framework 3.5.

The ConfigurationManager class enables a Web or Windows application to access machine, application, and user configuration files. The name and location of the configuration files depend on whether you are working with a Web application or a Windows console application. For more information, see Application Configuration Files.

You can use this example to build a console application by simply replacing the program file in a console project. For information about how to build a console application, see How to: Create a Console Application.

Note To compile this example, you must add a reference to the System.Configuration assembly to the project.

To get the example source code, click one of the following links

:

 

        · Using ConfigurationManager (C#).

· Using ConfigurationManager (VB.NET).

 

We appreciate your comments and feedback.

Description

The example shows how to build a console application that exercises the main capabilities of the ConfigurationManager class. The example works with a custom configuration section that stores color information for the background and foreground of the console window. The custom configuration section lets you do the following:

  • Create custom information to store in several configuration files, such as the configuration file for roaming users and the default application configuration file that applies to all users.
  • Specify what configuration file and where in the configuration hierarchy the section can be stored or overridden.

The example contains the following classes:

  • ConsoleSection. This class defines a custom section that the application uses to create custom configuration sections at the specified level in the configuration hierarchy (that is, in the appropriate configuration files). This enables users who have sufficient access rights to make changes to the configuration files.
  • ConsoleElement. This class is used by CustomSection. This element stores background and foreground colors that the application applies to the console window. The console color information is stored in different configuration files. The values used to define colors depend on which file the color settings are stored in.
  • UsingConfigurationManager. This class is the example core class and is used by the application to exercise the main ConfigurationManager capabilities.

UsingConfigurationManager performs the following tasks:

· Gets the roaming configuration file associated with the application.

· Gets the application configuration file associated with the application.

· Accesses a specified configuration file through mapping.

· Accesses the machine configuration file through mapping.

· Reads a specified configuration section.

· Reads the connectionStrings section.

· Reads or writes the appSettings section.

  • ApplicationMain. This class contains the application Main function and is used to obtain user's input and to provide feedback. It exercises the ConfigurationManager by calling its methods, based on the user's selection.

For a complete example, see the attached files.

We look forward to your comments and feedback.

Michael Miele

ASP.NET User Education
This posting is provided "AS IS" with no warranties, and confers no rights.

Give Your Feedback on the Documentation

Help us improve the developer documentation by taking the Visual Studio and .NET Framework Content Survey. This survey will give us a better understanding of the type of applications you are developing as well as how you use Help and how we can improve it. The survey takes only 10 minutes, and we appreciate your feedback.

ConfigurationManager.zip

Comments

  • Anonymous
    November 10, 2008
    How can we extend the configuration manager class to read configuration from a SQLServer table?
  • Anonymous
    November 10, 2008
    The short answer is no you cannot.  The longer explanation is that the ConfigurationManager class is part of the API used to manage configuration information stored in configuration files. This API provides an object model for the configuration elements stored in these files and rely on the configuration file hierarchy for merging configuration information and for allowing or denying access to this information.