Edit

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

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

    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.

    //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.

The following example shows an application settings file that defines two application-scoped settings and two user-scoped settings. You need to add the names for settings that your created as entries under the <configSections> element at the top of the file.

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