共用方式為


如何:建立應用程式設定

您可以使用 Managed 程式碼來建立新的應用程式設定,並將設定繫結至表單或表單控制項的屬性,以便在執行階段自動載入及儲存這些設定。

在下列程序中,您將手動建立一個衍生自 ApplicationSettingsBase 的包裝函式類別。 針對這個類別,您會加入可公開存取的屬性,以代表要公開的每一個應用程式設定。

您也可以在 Visual Studio 設計工具中,使用最少的程式碼來執行這個程序。 另請參閱 如何:使用設計 工具建立應用程式設定。

以程式設計方式建立新的應用程式設定

  1. 將新類別加入您的專案中,並且重新命名。 針對此程式,我們將呼叫這個類別 MyUserSettings 。 變更類別定義,使該類別衍生自 ApplicationSettingsBase

  2. 在包裝函式類別上為每一個所需的應用程式設定定義屬性,並且依據設定的範圍使用 ApplicationScopedSettingAttributeUserScopedSettingAttribute 來套用該屬性。 如需設定範圍的詳細資訊,請參閱 應用程式設定概觀 。 現在,您的程式碼應該如下所示:

    using System;
    using System.Configuration;
    using System.Drawing;
    
    public class MyUserSettings : ApplicationSettingsBase
    {
        [UserScopedSetting()]
        [DefaultSettingValue("white")]
        public Color BackgroundColor
        {
            get
            {
                return ((Color)this["BackgroundColor"]);
            }
            set
            {
                this["BackgroundColor"] = (Color)value;
            }
        }
    }
    
    Imports System.Configuration
    
    Public Class MyUserSettings
        Inherits ApplicationSettingsBase
        <UserScopedSetting()> _
        <DefaultSettingValue("white")> _
        Public Property BackgroundColor() As Color
            Get
                BackgroundColor = Me("BackgroundColor")
            End Get
    
            Set(ByVal value As Color)
                Me("BackgroundColor") = value
            End Set
        End Property
    End Class
    
  3. 在應用程式中建立這個包裝函式類別的執行個體。 這個執行個體通常會是主要表單的私用成員。 定義類別之後,您需要將類別繫結至屬性;在本例中,即為表單的 BackColor 屬性。 您可以在表單的 Load 事件處理常式中完成此作業。

    MyUserSettings mus;
    
    private void Form1_Load(object sender, EventArgs e)
    {
        mus = new MyUserSettings();
        mus.BackgroundColor = Color.AliceBlue;
        this.DataBindings.Add(new Binding("BackColor", mus, "BackgroundColor"));
    }
    
    Dim Mus As MyUserSettings
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Mus = New MyUserSettings()
        Mus.BackgroundColor = Color.AliceBlue
        Me.DataBindings.Add(New Binding("BackColor", Mus, "BackgroundColor"))
    End Sub
    
  4. 如果您提供在執行階段變更設定的方式,則需要在關閉表單時將使用者的目前設定儲存至磁碟,否則這些變更將會遺失。

    //Make sure to hook up this event handler in the constructor!
    //this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
        void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            mus.Save();
        }
    
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Mus.Save()
    End Sub
    

    您現在已成功建立新的應用程式設定,並將設定繫結至指定的屬性。

下列範例顯示定義兩個應用程式範圍設定和兩個使用者範圍設定的應用程式佈建檔。 您必須在檔案頂端的 < configSections > 元素底下,新增您建立為專案之設定的名稱。

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

.NET Framework 安全性

預設的設定提供者 LocalFileSettingsProvider 將資訊以純文字格式保存於組態檔。 這會將安全性限定為目前使用者之作業系統所提供的檔案存取安全性。 因此,您必須小心使用儲存在組態檔中的資訊。 例如,應用程式設定的常見用途之一,是儲存指向應用程式資料存放區的連接字串。 不過,基於安全性考量,這類字串不應包含密碼。 如需連接字串的詳細資訊,請參閱 SpecialSetting

另請參閱