次の方法で共有


Configuration API: Using System.Configuration.ConfigurationManager

Summary: 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.

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 classes described next and shown in the following picture:

ConfigurationManagerSample

  • 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. It 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.

ConfigurationManager.zip

Comments