共用方式為


appSettings Element (General Settings Schema) 

Contains custom application settings, such as file paths, XML Web service URLs, or any information that is stored in the.ini file for an application.

<appSettings
   file="relative file name" >
</appSettings>

Attributes and Elements

The following sections describe attributes, child elements, and parent elements.

Attributes

Element Description

file

Optional String attribute.

Specifies a relative path to an external file that contains custom application configuration settings. The specified file contains the same kind of settings that are specified in the appSettings add, clear, and remove attributes and uses the same key/value pair format as those elements.

The path that is specified is relative to the local configuration file. The runtime ignores the attribute, if the specified file cannot be found.

Because any changes to the Web.config file cause the application to restart, using a separate file allows users to modify values that are in the appSettings section without causing the application to restart. The contents of the separate file are merged with the appSettings section in the Web.config file. This functionality is limited to the appSettings attribute.

Note   In the .NET Framework version 2.0, you can now include configuration settings in a separate file for all configuration elements that support the configSource attribute. However, when you use the configSource attribute, you must move the entire section to the separate file because there is no merging of element settings. There is a one-time write to the Web.config file when using the configSource attribute. This causes the application to restart, but subsequent updates to the section are written directly to the separate file and do not cause subsequent application restarts. For more information, see ConfigSource.

Inherited attributes

Optional attributes.

Attributes inherited by all section elements.

Child Elements

Element Description

add

Optional element.

Adds a custom application setting as a name/value pair to the application settings collection.

clear

Optional element.

Removes all references to inherited custom application settings and allows only the references that are added by the current add attribute.

remove

Optional element.

Removes a reference to an inherited custom application setting from the application settings collection.

Parent Elements

Element Description

configuration

Specifies the required root element in every configuration file that is used by the common language runtime and the .NET Framework applications.

system.web

Specifies the root element for the ASP.NET configuration settings in a configuration file and contains configuration elements that configure ASP.NET Web applications and control how the applications behave.

Remarks

The appSettings element stores custom application configuration information, such as file paths, XML Web service URLs, or any information that is stored in the.ini file for an application. The key/value pairs that are specified in the appSettings element can be accessed in code using the ConfigurationSettings class.

You can use the file attribute to specify a configuration file that provides additional settings or overrides the settings that are specified in the appSettings element. You can use the file attribute in source control team development scenarios, such as when a user wants to override the project settings that are specified in an application configuration file. Configuration files that are specified in a file attribute must have the appSettings element rather than configuration element as the root node.

In the .NET Framework version 2.0 applications, store database connection strings in the connectionStrings Element (ASP.NET Settings Schema) collection instead of the application settings collection.

Default Configuration

The following default appSettings element is not explicitly configured in the Machine.config file or in the root Web.config file. However, it is the default configuration that is returned by application.

   <appSettings file="">
      <settings>
         <clear />
      </settings>
   </appSettings>

Example

Configuration files that are specified in a file attribute must have the appSettings element rather than the configuration element as the root node.

The following code example demonstrates how to use the correct form for a configuration file that is specified in the file attribute.

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="Application1" value="MyApplication1" />
<add key="Setting1" value="MySetting" />
</appSettings>

The following code example shows how to define a custom application setting in a configuration file.

<configuration>
    <appSettings>
        <add key="Application Name" value="MyApplication" />
    </appSettings>
</configuration>

Element Information

Configuration section handler

AppSettingsSection

Configuration member

AppSettings

AppSettings

AppSettings

Configurable locations

Machine.config

Root-level Web.config

Application-level Web.config

Virtual or physical directory–level Web.config

Requirements

Microsoft Internet Information Services (IIS) 5.0, 5.1, or 6.0

The .NET Framework version 1.0, 1.1, or 2.0

Microsoft Visual Studio 2003 or Visual Studio 2005

See Also

Reference

configuration Element (General Settings Schema)
add Element for appSettings (General Settings Schema)
clear Element for appSettings (General Settings Schema)
remove Element for appSettings (General Settings Schema)

Concepts

Securing ASP.NET Configuration
ASP.NET Configuration Scenarios

Other Resources

General Configuration Settings (ASP.NET)
ASP.NET Configuration Settings
ASP.NET Configuration Files