创建应用程序设置

如何:创建应用程序设置

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

猜你喜欢

转载自blog.csdn.net/weixin_42032900/article/details/82494357