Share via


Configuration.AppSettings 屬性

定義

取得此 AppSettingsSection 物件所套用的 Configuration 物件組態區段。

public:
 property System::Configuration::AppSettingsSection ^ AppSettings { System::Configuration::AppSettingsSection ^ get(); };
public System.Configuration.AppSettingsSection AppSettings { get; }
member this.AppSettings : System.Configuration.AppSettingsSection
Public ReadOnly Property AppSettings As AppSettingsSection

屬性值

AppSettingsSection 物件,表示此 Configuration 物件所套用的 appSettings 組態區段。

範例

下列程式代碼範例示範如何使用 AppSettings 屬性。

try
{
    AppSettingsSection appSettings =
        config.AppSettings as AppSettingsSection;
    Console.WriteLine("Section name: {0}",
            appSettings.SectionInformation.SectionName);

    // Get the AppSettings section elements.
    Console.WriteLine();
    Console.WriteLine("Using AppSettings property.");
    Console.WriteLine("Application settings:");
    // Get the KeyValueConfigurationCollection 
    // from the configuration.
    KeyValueConfigurationCollection settings =
      config.AppSettings.Settings;

    // Display each KeyValueConfigurationElement.
    foreach (KeyValueConfigurationElement keyValueElement in settings)
    {
        Console.WriteLine("Key: {0}", keyValueElement.Key);
        Console.WriteLine("Value: {0}", keyValueElement.Value);
        Console.WriteLine();
    }
}
catch (ConfigurationErrorsException e)
{
    Console.WriteLine("Using AppSettings property: {0}",
        e.ToString());
}
Try
    Dim appSettings As AppSettingsSection = TryCast(config.AppSettings, AppSettingsSection)
    Console.WriteLine("Section name: {0}", appSettings.SectionInformation.SectionName)

    ' Get the AppSettings section elements.
    Console.WriteLine()
    Console.WriteLine("Using AppSettings property.")
    Console.WriteLine("Application settings:")
    ' Get the KeyValueConfigurationCollection 
    ' from the configuration.
    Dim settings As KeyValueConfigurationCollection = config.AppSettings.Settings

    ' Display each KeyValueConfigurationElement.
    For Each keyValueElement As KeyValueConfigurationElement In settings
        Console.WriteLine("Key: {0}", keyValueElement.Key)
        Console.WriteLine("Value: {0}", keyValueElement.Value)
        Console.WriteLine()
    Next keyValueElement
Catch e As ConfigurationErrorsException
    Console.WriteLine("Using AppSettings property: {0}", e.ToString())
End Try

備註

AppSettings使用 屬性來存取和變更appSettings開啟組態檔中預設定義的組態區段。

適用於