Microsoft KB Archive/815786

= How to store and retrieve custom information from an application configuration file by using Visual C# =

Article ID: 815786

Article Last Modified on 5/12/2007

-

APPLIES TO


 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





For a Microsoft Visual Basic .NET version of this article, see 313405.

 This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Configuration



IN THIS TASK

 * SUMMARY
 * Requirements
 * Create a Console Application That Reads the Content of a Configuration File
 * Complete Code Listing
 * Complete Configuration File Listing (ConConfig.exe.config)
 * Verify That It Works
 * Troubleshoot
 * REFERENCES



SUMMARY
This article describes how to store custom information in a configuration (.config) file that you can retrieve later during run time by its associated application. This is helpful when you must define data that is associated with an application.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005

This article assumes that you are familiar with the following topics:
 * Extensible Markup Language (XML)
 * .NET configuration files

back to the top

Create a Console Application That Reads the Content of a Configuration File
You can store application settings in the configuration file that is associated with the application. Configuration files are saved in XML format.

The System.Configuration and the System.Collections.Specialized namespaces in the Microsoft .NET Framework include the necessary classes to retrieve information from a .NET application configuration file during run time.

To create a console application that reads the contents of an associated configuration file during run time, follow these steps:  Start Visual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project. Click Visual C# Projects under Project Types, and then click Console Application under Templates. Name the project ConConfig. By default, Visual C# .NET creates a class that is named Class1.

Note In Visual Studio 2005, click Visual C# under Project Types, and then click Console Application under Templates. Name the project ConConfig. By default, Visual C# .NET creates a class that is named Program. Make sure that the Solution Explorer window is visible. If it is not visible, press the CTRL+ALT+L key combination. In Solution Explorer, right-click the project name, and then click Add New Item. In the Add New Item list, click to select XML File.</li> In the Name text box, type App.config, and then click Open.

Note In Visual Studio 2005, click Add.</li>  You can use an application configuration file to collect custom application settings that you save in key/value format. You can include   elements in the <appSettings> section of an associated configuration file. Each key/value pair has one   element. An   element has the following format: <add key=&quot;Key0&quot; value=&quot;0&quot; /> Add an <appSettings> section with   elements to the configuration file between the   and   tags.

For example, the following configuration file includes an <appSettings> section that specifies three key/value pairs: <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <appSettings> <add key=&quot;Key0&quot; value=&quot;0&quot; /> <add key=&quot;Key1&quot; value=&quot;1&quot; /> <add key=&quot;Key2&quot; value=&quot;2&quot; /> </appSettings> </li>  In Solution Explorer, double-click Class1.cs to display the code window. Add the following statements to your code module.

Note These statements must appear before any other statements in the file. using System.Configuration; using System.Collections.Specialized; </li>  To hold the value from a configuration file key in the <appSettings> section of the configuration file, declare a string variable in the Main section as follows: string sAttr ; </li>  To retrieve a value for a specified key from the <appSettings> section of the configuration file, use the Get method of the AppSettings property of the ConfigurationSettings class. The ConfigurationSettings class is in the System.Configuration namespace. When the AppSettings.Get method receives a string input parameter that contains a key, the application retrieves the value that is associated with the key.

The following code retrieves the value for the Key0 attribute from the associated configuration file. The code then places this value in the sAttr string variable. If a key does not exist for this value, nothing is stored in sAttr. sAttr = ConfigurationSettings.AppSettings.Get(&quot;Key0&quot;); </li>  To display the value that the application retrieves in the Console window, use Console.WriteLine as follows: Console.WriteLine(&quot;The value of Key0 is &quot;+sAttr); </li>  You can use one reference to the AppSettings property to retrieve all the key/value pairs in the'<appSettings>' section. When you use the AppSettings property, the application returns all associated key/value pairs. These pairs are stored in a NameValueCollection type. The NameValueCollection contains key/value entries for each key that the application retrieves. The NameValueCollection class is in the System.Collections.Specialized namespace. NameValueCollection sAll ; sAll = ConfigurationSettings.AppSettings; </li>  The AllKeys property of NameValueCollection references a string array that has an entry for each key that the application retrieves. Use a foreach construction to iterate through the AllKeys array to access each key that the application retrieves. Each key entry in AllKeys is a string data type.

Inside the foreach construction, use Console.WriteLine to display the key and its associated value in the Console window. The current key that the application processes is in &quot;s&quot;. Use this as an index in the sAllNameValueCollection to obtain its associated value. foreach (string s in sAll.AllKeys) Console.WriteLine(&quot;Key: &quot;+ s + &quot; Value: &quot; + sAll.Get(s)); Console.ReadLine; </li></ol>

back to the top

Complete Code Listing
using System; using System.Configuration; using System.Collections.Specialized;

namespace ConConfig {  class Class1 {     static void Main(string[] args) {        string  sAttr ; // Read a particular key form config file sAttr = ConfigurationSettings.AppSettings.Get(&quot;Key0&quot;); Console.WriteLine(&quot;The value of Key0: &quot; + sAttr); // read all the keys from the config file NameValueCollection sAll ; sAll = ConfigurationSettings.AppSettings;

foreach (string s in sAll.AllKeys) Console.WriteLine(&quot;Key: &quot;+ s + &quot; Value: &quot; + sAll.Get(s)); Console.ReadLine; }  } } back to the top

Complete Configuration File Listing (ConConfig.exe.config)
<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <appSettings> <add key=&quot;Key0&quot; value=&quot;0&quot; /> <add key=&quot;Key1&quot; value=&quot;1&quot; /> <add key=&quot;Key2&quot; value=&quot;2&quot; /> </appSettings> back to the top

Verify That It Works
Press F5 to run the code. The Console window should display the key/value pairs from the '<appSettings>' section of the associated configuration file as follows: The value of Key0: 0 Key: Key0 Value:0 Key: Key1 Value:1 Key: Key2 Value:2 back to the top

Troubleshoot
<ul> The configuration file is saved in XML format. Make sure that you follow all XML syntax rules. Remember that XML is case-sensitive. If the XML is not well formed, or if an element is misspelled, you receive a System.Configuration.Configuration exception.

For example, if you add the key attribute of an   element with an uppercase &quot;K&quot; instead of a lowercase &quot;k,&quot; or if the <appSettings> section appears as <AppSettings> (with an uppercase &quot;A&quot; instead of a lowercase &quot;a&quot;), you receive an error message.</li> The configuration file must be saved in the same folder as its associated application.</li> You must use the following syntax for the configuration file name:

. .config

where  is the name of the application,   is the type of application (for example, .exe), and .config is the required suffix.</li></ul>

back to the top

<div class="references_section">