Windows Forms 2.0 Programming (Microsoft .NET Development Series)

The settings system offered by Windows Forms is comprehensive, but there are alternative data stores you may need to consider, including the Registry and special folders.

The Registry

The Registry was the place to keep read-write application and roaming-user settings from Windows 3.1 through Windows NT (it has fallen out of favor in more recent versions of Windows). The Registry is a hierarchical machinewide storage of arbitrary name/value pairs split into application and roaming-user localities based on the path to the value. The Registry Editor (regedit.exe) is a built-in tool for setting and updating Registry values, as shown in Figure 15.25.[17]

[17] Be careful when editing Registry values. You're working on live data that's used by the entire system. One wrong move and you're reinstalling the OS; there's no Undo!

Figure 15.25. The Registry Editor

The Registry is used a lot by Win32 applications, including the Explorer shell, so you can find yourself reading and writing Registry values whether you use it to store your own application's settings.

The Registry is composed of several special top-level folders known as hive keys, each of which has a well-known name and provides access to a broad category of settings. The Registry class (from the Microsoft.Win32 namespace) exposes a one-to-one mapping of properties to these hive keys, as listed in Table 15.1.

Table 15.1. Registry Properties and Hive Key Names

Registry Class Static Property

Registry Hive Key Name

Registry.ClassesRoot

HKEY_CLASSES_ROOT

Registry.CurrentConfig

HKEY_CURRENT_CONFIG

Registry.CurrentUser

HKEY_CURRENT_USER

Registry.DynData

HKEY_DYN_DATA[*]

Registry.LocalMachine

HKEY_LOCAL_MACHINE

Registry.PerformanceData

HKEY_PERFORMANCE_DATA

Registry.Users

HKEY_USERS

[*] Win9x only

It's beneath the following recommended Registry path that Microsoft recommends storing user settings, under HKEY_CURRENT_USER:

HKEY_CURRENT_USER\Software\companyName\productName\productVersion

A path like this is known as a subkey in Registry parlance, and the following is an example:

HKEY_CURRENT_USER\Software\elvis\RegistrySample\1.0.0.0

Coincidentally, the variable values are exactly the same values provided by Application.CompanyName, Application.ProductName, and Application.Version, so you can construct a top-level key name using the following:

string key = string.Format( @"Software\{0}\{1}\{2}", Application.CompanyName, Application.ProductName, Application.ProductVersion);

Whereas keys are like folders, any hive key or subkey in the Registry is a container for one or more named values, with a name of null denoting the default value of a key. These values can be of several types, including string, unsigned integer, and arbitrary bytes. Although you should avoid using the Registry for application settings in general, one very good reason to use it is to enable tight shell integration for your Windows Forms applications, a topic discussed in Appendix F: Document Management.

Special Folders

Special folders are folders that Windows designates as having a special purpose. For example, the folder where programs are installed by default is a special folder:

// Generally "C:\Program Files" string programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

There are three special folders for settings: one each for the application, user, and roaming-user localities. You are better off accessing these locations through the special folders than by hard-coded paths because localized versions of Windows might change the names of the actual folders. Table 15.2 shows them, along with some sample paths running on Windows XP.

Table 15.2. Settings-Oriented Special Folders, Locality, and Examples

SpecialFolder Enum Value

Locality

Example Path

CommonApplicationData

Application

C:\Documents and Settings\All Users\Application Data

LocalApplicationData

User

C:\Documents and Settings\<user>\Local Settings\Application Data

ApplicationData

Roaming user

C:\Documents and Settings\<user>\Application Data

The special folder serves as the top-level folder in the folder under which applications can store application, user, and roaming-user settings (just as Registry.LocalMachine and Registry.CurrentUser serve as the top level for application and roaming-user settings). Under that folder, an application is expected to construct a subfolder to avoid colliding with other applications or even versions of itself. The subfolder name has the following format:

specialFolder\companyName\productName\productVersion

For example:

C:\Documents and Settings\elvis\Local Settings\Application Data\Sells Brothers, Inc.\My Settings Test\1.0.1124.33519

And just as the Application object provides shortcut access to Registry keys via properties, it also provides shortcut access to prefabricated folder names and precreated folders via the CommonAppDataPath, LocalUserAppDataPath, and UserAppDataPath properties.[18]

[18] The user.config file is saved to the path stored in LocalUserAppDataPath.

Категории