Settings Object


Settings Object

Location

My.Settings

Description

Use the My.Settings object to manage application-specific and user-specific settings for an application. The 2002 and 2003 releases of Visual Basic included a generic configuration system for projects, but it had certain deficiencies, including the inability to manage strongly typed data. The new configuration system included with Visual Basic 2005 resolves many of the deficiencies present in those earlier releases.

Settings are initially managed in Visual Studio through the new Settings panel of the project's Properties window. (Access this window by double-clicking on the "My Project" item in the Solution Explorer or by selecting Properties from Visual Studio's Project menu.) This form lets you add configuration settings to a project, including the name, data type, scope (application or user), and initial value of each setting.

The name of each setting automatically becomes a property of the My.Settings object. For example, if you add an Integer setting called AlarmDuration to the list of settings, it can be accessed in your code as:

     My.Settings.AlarmDuration 

and it will be strongly typed as an Integer. If it is a user-scoped value, the code can update its value through direct assignment.

     My.Settings.AlarmDuration = 5 

The settings are automatically saved when the programs exits, depending on the setting of the My.Application.SaveMySettingsOnExit property.

When developing an application in Visual Studio, the settings defined through the Project Properties window are stored in the app.config file for the project.

Once your application is deployed, the application-scoped settings and the user-scoped settings for each user are stored in different files. Each user's settings are usually stored in:

     C:\DocumentsandSettings\LocalSettings\ApplicationData\companyName\hashedAppDomain\version\user.config 

where companyName is the assembly-defined company name for your project, hashedAppDomain is constructed from the application-related values, and version is the four-part, dot-delimited assembly version number. It's somewhat complex, but it is also uniquely identifiable.

The application-scoped settings (and the default versions of all user-scoped setting) are stored in the Settings.settings file, found in the same folder as the assembly file.

Public Members

Each setting added to your project through the Project Properties window becomes a property in the My.Settings object. In addition to these, there are several members used to manage the settings.

Member

Description

GetPreviousVersion

Method. Retrieves the value for a specific setting as stored in the settings file for a previous version of the assembly.

Reload

Method. Reloads all settings to the values they contained before this instance of the application was started. This is useful for discarding changes made during the current application session.

Reset

Method. Reloads all settings based on their default values. This discards any changes made since the application (or a specific version) was installed.

Save

Method. Immediately saves all modifications made to settings.


Usage at a Glance

  • Settings that have an application-level scope are read-only and cannot be modified through My.Settings. Settings with user-level scope can be modified.

  • The settings functionality is extensible, so that additional providers that manage the settings can be added.

Related Framework Entries

  • System.Configuration.ApplicationSettingsBase

See Also

Forms Object, My Namespace, Resources Object, SaveMySettingsOnExit Property, WebServices Object




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net