Skip to content

Latest commit

 

History

History
87 lines (69 loc) · 6.03 KB

how-to-create-application-settings.md

File metadata and controls

87 lines (69 loc) · 6.03 KB
title ms.date dev_langs helpviewer_keywords ms.assetid description
How to: Create Application Settings
03/30/2017
csharp
vb
application settings [Windows Forms], Windows Forms
application settings [Windows Forms], creating
1e7aa347-af75-41e5-89ca-f53cab704f72
Learn how to create application settings and bind them to properties on your form or your form's controls.

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 xref:System.Configuration.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 xref:System.Configuration.ApplicationSettingsBase.

  2. Define a property on this wrapper class for each application setting you require, and apply that property with either the xref:System.Configuration.ApplicationScopedSettingAttribute or xref:System.Configuration.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:

    [!code-csharpApplicationSettings.Create#1] [!code-vbApplicationSettings.Create#1]

  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 xref:System.Windows.Forms.Form.BackColor%2A property of your form. You can accomplish this in your form's Load event handler.

    [!code-csharpApplicationSettings.Create#2] [!code-vbApplicationSettings.Create#2]

  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.

    [!code-csharpApplicationSettings.Create#3] [!code-vbApplicationSettings.Create#3]

    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, xref:System.Configuration.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 xref:System.Configuration.SpecialSetting.

See also