The .NET Framework 2.0 allows you to create and access values held between application execution sessions. These values are called settings. Settings can represent user preferences or valuable information that the application needs. For example, you can create a series of settings that store user preferences for an application’s color scheme. Or, store the connection string that specifies a database used by the application. Settings allow you to keep critical application information out of code and create profiles that store user preferences individually.
Throughout this post, you’ll learn the difference between application and user settings, create new design-time settings, access settings at runtime, and even incorporate multiple sets of settings into your application.
The settings have four properties:
- Name: The Name property is the name used to access the value of the run-time configuration.
- Type: Type is the type of .NET Framework that the configuration represents. The configuration can be of any type. For example, a setting that maintains a user’s color preference may be of type System.Color.
- Scope: The Scope property represents how a configuration can be accessed at run time. There are two possible values for the Scope property: Application and User. They will be discussed later in this section.
- Value: The Value property represents the value returned at the time the configuration is accessed. The value will be of the type represented by the Type property.
Most properties are self-explanatory. Name, Type, and Value are concepts that should be familiar to most programmers. The Scope property requires a little more explanation. Configurations have two possible scopes: application scope and user scope. Settings with application scope represent those used by the application regardless of user preferences, whereas user-scoped settings are generally less important to the application itself and are more likely to be associated with preferences or other non-critical values .
The crucial distinction between application-scoped and user-scope configurations is that the latter are read / write at run time and their values can be changed and saved in code. Application scope settings are read-only at run time. Although they are read, they can not be recorded. Settings with application scope can be changed only at design time or by manually modifying the settings file.
Reading settings at run time
At runtime, you can read both application scope and user scope settings with namespace properties. The Properties namespace exposes all the default project settings by using the Properties.Settings.Default object. When writing code with settings, all settings appear in IntelliSense and are of the high security type. So, if there is a configuration of type System.Drawing.Color, for example, you can use it without having to convert it first, as shown in the following example:
this.BackColor = Properties.Settings.Default.myColor;