Dela via


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.

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.

You can also perform this procedure using minimal code in the Visual Studio designer. How to: Create Application Settings Using the Designer
How to: Create Application Settings Using the Designer
How to: Create Application Settings Using the Designer
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. For this procedure, we will call this class MyUserSettings. Change the class definition so that the class derives from ApplicationSettingsBase.

  2. 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:

    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
    
    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;
            }
        }
    }
    
  3. Create an instance of this wrapper class in your application. It will commonly be a private member of the main form. Now that you have defined your class, you need to bind it to a property; in this case, the BackColor property of your form. You can accomplish this in your form's Load event handler.

    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
    
    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"));
    }
    
  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.

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Mus.Save()
    End Sub
    
    //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();
            }
    

    You have now successfully created a new application setting and bound it to the specified property.

Security

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. For more information about connection strings, see SpecialSetting.

See Also

Tasks

How to: Validate Application Settings

Concepts

Application Settings Overview

Reference

SpecialSettingAttribute

LocalFileSettingsProvider