Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

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

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

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.