A new class in the Visual Basic 2005 environment improves the important task of configuring settings.
WinForms applications need data to establish the right runtime settings when they execute. It's smart to keep this data separate from code so that changes in application settings don't force recompilation. Previous incarnations of Visual Basic stored data in various historically popular locations, such as INI files, the Windows Registry, or in config files. And the .NET Framework 1.0 added app.config files to this mix (XML files used to store application settings).
Problems with these methods include one or more of the following:
- Settings may not be type-safe (or strongly typed at all);
- Considerable coding effort is required to read or write to such files;
- Using registry entries means applications must have permissions to read and/or write to the registry, or;
- User-scope settings are hard to maintain.
This new architecture addresses these problems. It makes it easy to store application- and user-scoped settings on the client machine. Using Visual Studio for Office or some other code editor, you can define a setting for any particular property simply by specifying the name, data type, and default value and by indicating if its scope covers the application or user. Under the hood this data is represented using XML in .config files. Application-scoped settings are read-only because they typically don't need to be rewritten; user-scoped settings may be read or written securely at run-time, even for applications that run under partial trust.
The task of adding a new setting in Visual Basic 2005 is eased significantly. There's no need to open a config file to enter that information. The Settings designer in the project properties form lets you add new settings, and select the associated name, type, and scope information for each one. The app.config file resides in the bin folder, named to match your application name. User scope settings are specific to individual users and are stored in a user.config file in that user's folder hierarchy under the Documents and Settings root-level folder on %SystemDrive% (usually C:\).
Microsoft has more details plus well-illustrated sample code and path examples on their Web site in the Visual Basic Developer Center. It appears under the heading Using My.Settings in Visual Basic 2005. For Visual Basic 2005 developers (or those looking for good reasons to upgrade to that version), this is a must-see item.
Ed Tittel is a full-time writer and trainer whose interests include XML and development topics, along with IT Certification and information security topics. E-mail Ed at email@example.com with comments, questions, or suggested topics or tools to review.
This was first published in October 2005