REGISTRY

The system registry is a hierarchical database that stores values for applications on the system. The hierarchy’s root is named MyComputer and is divided into the several subtrees that are also called hives. Which hives are available depends on your operating system. The following table summarizes the most commonly available hives. (The “HKEY” part of each name stands for “hive key.”)

REGISTRY BRANCH CONTAINS
HKEY_CLASSES_ROOT Definitions of types or classes, and properties associated with those types.
HKEY_CURRENT_CONFIG Information about the system’s current hardware configuration.
HKEY_CURRENT_USER The current user’s preferences (such as environment variable settings, program group information, desktop settings, colors, printers, network connections, and preferences specific to applications). Each user has separate HKEY_CURRENT_USER values. This is usually the subtree where a Visual Basic application stores and retrieves its settings.
HKEY_DYN_DATA Performance data for Windows 95, 98, and Me. (Yes, this is a bit outdated but this hive is still there.)
HKEY_LOCAL_MACHINE Information about the computer’s physical state including bus type, system memory, installed hardware and software, and network logon and security information.
HKEY_USERS Default configuration information for new users and the current user’s configuration.

Depending on your operating system, the registry may also contain the unsupported keys HKEY_PERFORMANCE_DATA, HKEY_PERFORMANCE_NLSTEXT, and HKEY_ PERFORMANCE_TEXT.

Many applications store information in the registry. The HKEY_CURRENT_USER subtree is particularly useful for storing individual users’ preferences and other configuration information.

Lately, the registry has gone out of style for saving configuration information. Microsoft now recommends that you store this kind of data locally within a user’s data storage area. This makes sense because it makes it easier to copy the settings (they’re just files), helps reduce clutter in the registry, and reduces the chances that mistakes will corrupt the registry. You can store this information in configuration files (see the section “Configuration Files” later in this chapter) or XML files.

Visual Basic provides two main ways to access the registry. First, you can use the Visual Basic native registry methods. Second, you can use the tools in the My.Computer.Registry namespace. These two methods are described in the following sections.

You can also use API functions to manipulate the registry. These are more complicated and not generally necessary because the My.Computer.Registry namespace contains some very powerful tools, so they are not described here.

Native Visual Basic Registry Methods

Visual Basic provides four methods for saving and reading registry values for a particular application: SaveSetting, GetSetting, GetAllSettings, and DeleteSetting.

The SaveSetting method saves a value into a registry key. This routine takes as parameters the name of the application, a section name, the setting’s name, and the setting’s value. For example, the following code saves the value stored in the MapDirectory variable in the RegistrySettings application’s Config section with the name MapDirectory:

SaveSetting("RegistrySettings", "Config", "MapDirectory",
    MapDirectory)

SaveSetting automatically creates the application and section areas in the registry if they don’t already exist.

This value is saved at the following registry location. This is all one name; it just doesn’t fit on one line here:

HKEY_CURRENT_USERSoftwareVB and VBA Program Settings
    RegistrySettingsConfigMapDirectory

If you use the Visual Basic SaveSetting, GetSetting, GetAllSettings, and DeleteSetting methods, you don’t need to worry about the first part of this registry path. You only need to remember the application name, section name, and setting name.


POWERFUL PRIVILEGES
Windows protects the registry so that you cannot inadvertently damage critical values. If you mess up some values, you can wreak havoc on the operating system, and even make the system unbootable.
To prevent possible chaos, newer versions of Windows don’t let you edit some parts of the registry without elevated privileges. Fortunately, the part of the registry used by these routines is accessible to normal users, so you don’t need elevated privileges to use SaveSetting, GetSetting, GetAllSettings, or DeleteSetting.

The GetSetting function retrieves a registry value. It takes as parameters the application name, section name, and setting name you used to save the value. It can optionally take a default value to return if the setting doesn’t exist in the registry. The following code displays the value saved by the previous call to SaveSetting. If no value is saved in the registry, it displays the string <none>.

MessageBox.Show(GetSetting("RegistrySettings", "Config", "MapDirectory",
 "<none>"))

The GetAllSettings function returns a two-dimensional array of name and value pairs for a registry section. The following code uses GetAllSettings to fetch the values stored in the RegistrySettings application’s Config section. It loops through the results, displaying the setting names and values.

Dim settings As String(,) = GetAllSettings("RegistrySettings", "Config")
For i As Integer = 0 To settings.GetUpperBound(0)
    Debug.WriteLine(settings(i, 0) & " = " & settings(i, 1))
Next i

If an application needs to use all of the settings in a section, GetAllSettings may be faster than using GetSetting repeatedly.

The DeleteSetting method removes a setting, a section, or an entire application’s setting area from the registry. The following code shows how to remove each of those kinds of items:

' Remove the RegistrySettings/Config/CurrentDirectory setting.
DeleteSetting("RegistrySettings", "Config", "CurrentDirectory")
 
' Remove the RegistrySettings/Config section.
DeleteSetting("RegistrySettings", "Config")
 
' Remove all of the RegistrySettings application's settings.
DeleteSetting("RegistrySettings")

NEATNESS COUNTS
As part of its uninstallation procedure, a program should remove any registry entries it has made. All too often, programs leave the registry cluttered with garbage. This not only makes it harder to figure out what real values the registry contains but can also slow the system down.
In an attempt to combat this problem, Microsoft is promoting XCopy compatibility, where applications store values in configuration files instead of the registry. Then you can easily copy and remove these files rather than modify the registry.

Example program RegistrySettings demonstrates each of Visual Basic’s Registry commands.

My.Computer.Registry

The My.Computer.Registry namespace provides objects that manipulate the registry. My.Computer.Registry has seven properties that refer to objects of type RegistryKey. The following table lists these objects and the corresponding registry subtrees:

MY.COMPUTER.REGISTRY PROPERTY REGISTRY SUBTREE
ClassesRoot HKEY_CLASSES_ROOT
CurrentConfig HKEY_CURRENT_CONFIG
CurrentUser HKEY_CURRENT_USER
DynData HKEY_DYNAMIC_DATA
LocalMachine HKEY_LOCAL_MACHINE
PerformanceData HKEY_PERFORMANCE_DATA
Users HKEY_USERS

REGISTRY RESTRICTIONS
Some parts of the registry are off limits to programs running as normal users in recent versions of Windows. Normal users can modify values in HKEY_CURRENT_USER, but to do more than look in other areas, a program would probably need to run with elevated privileges. For more information on privilege elevation, see Microsoft’s article “User Account Control Step-by-Step Guide” at http://technet.microsoft.com/library/cc709691.aspx.

The program can use these RegistryKey objects to work with the corresponding registry subtree. The following table describes the most useful properties and methods provided by the RegistryKey class:

PROPERTY OR METHOD PURPOSE
Close Closes the key and writes it to disk if it has been modified.
CreateSubKey Creates a new subkey or opens an existing subkey within this key.
DeleteSubKey Deletes the specified subkey. This method will delete the subkey if it contains values, but not if it contains other subkeys. The subkey to be deleted need not be a direct child of this key. For example, the following code uses the CurrentUser RegistryKey object to delete the descendant key SoftwareVB and VBA Program SettingsMyComputerRegistryConfig: My.Computer.Registry.CurrentUser.DeleteSubKey("SoftwareVB and VBA Program SettingsRegistrySettingsConfig")
DeleteSubKeyTree Recursively deletes a subkey and any child subkeys it contains. The subkey to be deleted need not be a direct child of this key.
DeleteValue Deletes a value from the key.
Flush Writes any changes to the key into the registry.
GetSubKeyNames Returns an array of strings giving subkey names.
GetValue Returns the value of a specified value within this key.
GetValueKind Returns the type of a specified value within this key. This can be Binary, DWord, ExpandString, MultiString, QWord, String, or Unknown.
GetValueNames Returns an array of strings giving the names of all of the values contained within the key.
Name Returns the key’s registry path.
OpenSubKey Returns a RegistryKey object representing a descendant key. Parameters give the subkey name, and indicate whether the returned RegistryKey should allow you to modify the subkey.
SetValue Sets a value within the key.
SubKeyCount Returns the number of subkeys that are this key’s direct children.
ToString Returns the key’s name.
ValueCount Returns the number of values stored in this key.

The following example opens the HKEY_CURRENT_USERSoftwareVB and VBA Program SettingsRegistrySettingsConfig key. It reads the CurrentDirectory value from that key using the default value C: and saves the result in the variable current_directory. It closes the key and then uses the DeleteSubKey method to delete the RegistrySettings application’s Config section.

' Open the application's Config subkey.
Dim config_section As Microsoft.Win32.RegistryKey =
    My.Computer.Registry.CurrentUser.OpenSubKey(
        "SoftwareVB and VBA Program SettingsRegistrySettingsConfig")
 
' Get the CurrentDirectory value.
Dim current_directory As String =
    CType(config_section.GetValue("CurrentDirectory", "C:"), String)
 
' Close the subkey.
config_section.Close()
 
' Delete the application's whole Config section.
My.Computer.Registry.CurrentUser.DeleteSubKey(
    "SoftwareVB and VBA Program SettingsRegistrySettingsConfig")

The following code shows the equivalent operations using the native registry methods of Visual Basic:

 ' Get the CurrentDirectory value.
Dim current_directory As String =
GetSetting("RegistrySettings", "Config", "CurrentDirectory", "C:")
 
' Delete the application's whole Config section.
DeleteSetting("RegistrySettings", "Config")

It is generally easier to use the native registry methods of Visual Basic. Those methods work only with values in the HKEY_CURRENT_USERSoftwareVB and VBA Program Settings Registry subtree, however. If you need to access keys and values outside of this subtree, you must use the My.Computer.Registry objects.

Example program MyComputerRegistry demonstrates many useful My.Computer.Registry operations. It does the same things as program RegistrySettings mentioned in the previous section except it uses My.Computer.Registry instead of Visual Basic’s native registry methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.252