.NET Framework General Reference
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.

configuration Element (General Settings Schema)
  appSettings Element (General Settings Schema)
<appSettings
   file="relative file name" >
</appSettings>

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.

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 ConfigurationManager 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 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>

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>

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, 6.0, or 7.0

The .NET Framework version 1.0, 1.1, or 2.0

Microsoft Visual Studio 2003, Visual Studio 2005, or Visual Studio 2008

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Regarding file attribute value restricted to OS File Path      Developer 3.0   |   Edit   |   Show History

The feature of "externalizing" sections of configuration file is a very useful concept. However , the fact that the file attribute of <appSettings> is restricted only to relative or physical paths is in a way limiting its use in web applications where virtual paths provide the location transparency for resources.

Is there any hook or callback mechanism provided by AppSettingsSection class ( or any ASP.NET infrastructure class) so that the file attribute can be specified as a virtual path (such as file="~/MyResources/X.config") in the root application's web.config file , but the value is translated into a physical path at runtime such as "D:\CustomResources\X.config" without requiring any physical writes to the web.config file (Just like XmlResolvers) ?

The idea is to leverage on the benefit of "changing configuration without application restarts" in the context of virtual paths for external config files.

Tags What's this?: Add a tag
Flag as ContentBug
RemoteSettings      Erik Wynne Stepp   |   Edit   |   Show History
This is less of a comment on the article than a comment on the feedback above.

There are security reasons for intentionally not making settings available remotely. These are the same reasons that ASP.NET blocks all *.config files. If you hosted your settings with a virtual path, your settings would then be exposed when they probably shouldn't be.

If you find that you absolutely must have settings stored remotely and you aren't concerned with the security implications of doing this, then a relatively simple solution might be to implement your own RemoteSettings custom configuration section which would be fairly easy to do.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
Page view tracker