1. 程式人生 > >建立應用程式設定

建立應用程式設定

如何:建立應用程式設定

7‎年‎03‎月‎30‎日
轉(Microsoft):https://docs.microsoft.com/zh-cn/dotnet/framework/winforms/advanced/how-to-create-application-settings

使用託管程式碼時,你可以建立新的應用程式設定並將其繫結窗體或窗體控制元件的屬性上,以便在執行時自動載入和儲存這些設定。Using managed code, you can create new application settings and bind them to properties on your form or your form’s controls, so that these settings are loaded and saved automatically at run time.

在以下過程中,手動建立從 ApplicationSettingsBase 派生的包裝類。In the following procedure, you manually create a wrapper class that derives from ApplicationSettingsBase. 對於此類,你可以為每個想要公開的應用程式設定新增可公開訪問的屬性。To this class you add a publicly accessible property for each application setting that you want to expose.

你還可以使用 Visual Studio 設計器中的最小程式碼執行該過程。

You can also perform this procedure using minimal code in the Visual Studio designer. 另請參閱如何: 使用設計器建立應用程式設定Also see How to: Create Application Settings Using the Designer.

以程式設計的方式建立新的應用程式設定To create new Application Settings programmatically

  1. 向你的專案新增新類,並對其重新命名。Add a new class to your project, and rename it.

    對於此過程中,我們將呼叫此類MyUserSettingsFor this procedure, we will call this class MyUserSettings. 更改類定義,以使類從 ApplicationSettingsBase 派生。Change the class definition so that the class derives from ApplicationSettingsBase.

  2. 為你需要的每個應用程式設定定義此包裝類的屬性,並將該屬性應用到 ApplicationScopedSettingAttributeUserScopedSettingAttribute,具體取決於設定的作用域。Define a property on this wrapper class for each application setting you require, and apply that property with either the ApplicationScopedSettingAttribute or UserScopedSettingAttribute, depending on the scope of the setting. 有關設定作用域的詳細資訊,請參閱應用程式設定概述For more information about settings scope, see Application Settings Overview. 現在,程式碼應如下所示:By now, your code should look like this:

    C# 複製
    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. 在你的應用程式中建立此包裝類的例項。Create an instance of this wrapper class in your application. 該例項通常為主窗體的私有成員。It will commonly be a private member of the main form. 對類進行定義後,你需要將其繫結到屬性;在這種情況下,是指繫結到你窗體的 BackColor 屬性。Now that you have defined your class, you need to bind it to a property; in this case, the BackColor property of your form. 可以完成此操作在窗體的Load事件處理程式。You can accomplish this in your form’s Load event handler.

    C# 複製
    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. 如果你在執行時提供了一種更改設定的方法,則關閉窗體時需要將使用者的當前設定儲存到磁碟,否則將丟失這些更改。If you provide a way to change settings at run time, you will need to save the user’s current settings to disk when your form closes, or else these changes will be lost.

    C# 複製
    //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
    

    現在,你已成功建立了一個新的應用程式設定並將其繫結到了指定的屬性中。You have now successfully created a new application setting and bound it to the specified property.

.NET Framework 安全性.NET Framework Security

預設的設定提供程式 LocalFileSettingsProvider 會將配置檔案的資訊視為純文字處理。The default settings provider, LocalFileSettingsProvider, persists information to configuration files as plain text. 這將限制由當前使用者由的作業系統提供的檔案訪問安全性的安全。This limits security to the file access security provided by the operating system for the current user. 因此,必須謹慎處理配置檔案中儲存的資訊。Because of this, care must be taken with the information stored in configuration files. 例如,應用程式設定一種常見的用法就是,儲存指向應用程式資料儲存的連線字串。For example, one common use for application settings is to store connection strings that point to the application’s data store. 但是,出於安全考慮,此類字串不應包括密碼。However, because of security concerns, such strings should not include passwords. 有關連線字串的詳細資訊,請參閱 SpecialSettingFor more information about connection strings, see SpecialSetting.

請參閱See Also

SpecialSettingAttribute
LocalFileSettingsProvider
應用程式設定概述Application Settings Overview
如何:驗證應用程式設定How to: Validate Application Settings