共用方式為


Application Settings Schema

Application settings allow a Windows Forms or ASP.NET application to store and retrieve application-scoped and user-scoped settings. A "setting", in this context, is any piece of information that may be specific to the application or specific to the current user - anything from a database connection string to the user's preferred default window size.

By default, application settings in a Windows Forms application uses the LocalFileSettingsProvider, which uses the .NET configuration system to store settings in an XML configuration file. For more information about the files use by application settings, see Application Settings Architecture.

Application settings defines the following elements as part of the configuration files it uses.

Element Description

<applicationSettings> Element

Contains all <setting> tags specific to the application.

<userSettings> Element

Contains all <setting> tags specific to the current user.

<setting> Element

Defines a setting. Child of either <applicationSettings> or <userSettings>.

<value> Element

Defines a setting's value. Child of <setting>.

<applicationSettings> Element

This element contains all <setting> tags that are specific to an instance of the application on a client computer. It defines no attributes.

<userSettings> Element

This element contains all <setting> tags that are specific to the user who is currently using the application. It defines no attributes.

<setting> Element

This element defines a setting. It has the following attributes.


Element Description

name

Required. The unique ID of the setting. Settings created through Visual Studio are saved with the name ProjectName.Properties.Settings.

serializedAs

Required. The format to use for serializing the value to text. Valid values are:

  • string. The value is serialized as a string using a TypeConverter.

  • xml. The value is serialized using XML serialization.

  • binary. The value is serialized as text-encoded binary using binary serialization.

  • custom. The settings provider has inherent knowledge of this setting, and will serialize and de-serialize it.

  • To use binary or custom serialization, you must define your own settings class and use the SettingsSerializeAsAttribute to specify binary or custom serialization.

<value> Element

This element contains the value of a setting.

Example

The following code example shows an application settings file that defines two application-scoped settings and two user-scoped settings.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="WindowsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        </sectionGroup>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="WindowsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <WindowsApplication1.Properties.Settings>
            <setting name="Cursor" serializeAs="String">
                <value>Default</value>
            </setting>
            <setting name="DoubleBuffering" serializeAs="String">
                <value>False</value>
            </setting>
        </WindowsApplication1.Properties.Settings>
    </applicationSettings>
    <userSettings>
        <WindowsApplication1.Properties.Settings>
            <setting name="FormTitle" serializeAs="String">
                <value>Form1</value>
            </setting>
            <setting name="FormSize" serializeAs="String">
                <value>595, 536</value>
            </setting>
        </WindowsApplication1.Properties.Settings>
    </userSettings>
</configuration>

See Also

Concepts

Application Settings Overview
Application Settings Architecture