Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 1.1
.NET Framework
 Accessing the appSettings Section
.NET Framework Developer's Guide
Accessing the appSettings Section

The .NET Framework provides a predefined configuration section called appSettings. The following example shows the declaration of the appSettings section as it appears in the machine configuration file (Machine.config).

<configuration>
      <!-- Configuration section declarations. -->
   <configSections>
     <!-- You do not need to declare the appSettings section. Machine.config ships with a declaration for this section. -->
      <section name="appSettings" 
               type="System.Web.Configuration.NameValueSectionHandler"/>
   </configSections>
</configuration>

The following example shows how to use an application setting in a configuration file using the built-in appSettings section.

<configuration>
  <!-- The following code uses the predefined appSettings section. -->
    <appSettings>
        <add key="Application Name" value="MyApplication" />
    </appSettings>
</configuration>

ConfigurationSettings.AppSettings is a special property that provides a shortcut to application settings defined in the <appSettings> section of the configuration file. The following example shows how to retrieve the application name defined in the previous configuration file example.

[Visual Basic]
    Public Sub ReadMyAppSettings()
        Dim appName As String

        appName = ConfigurationSettings.AppSettings("Application Name")

        Console.WriteLine()
        Console.WriteLine("Reading AppSettings")
        Console.WriteLine("Application Name: " & appName)
    End Sub
[C#]
public void ReadMyAppSettings()
{
    string appName = ConfigurationSettings.AppSettings["Application Name"];

    Console.WriteLine();
    Console.WriteLine("Reading AppSettings");
    Console.WriteLine("Application Name: " + appName);
}

See Also

Configuration Section Settings | Configuration Sections Schema

© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker