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 six subtrees described in the following table.

Open table as spreadsheet

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.

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.

Visual Basic provides two main ways to access the Registry. First, you can use the Visual Basic native Registry methods: SaveSetting, GetSetting, GetAllSettings, and DeleteSetting. 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 (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 m_CurrentDirectory variable in the ImageOrganizer application’s Config section with the name CurrentDirectory:

  SaveSetting("ImageOrganizer", "Config", "CurrentDirectory", m_CurrentDirectory) 

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_USER\Software\VB and VBA Program Settings\     ImageOrganizer\Config\CurrentDirectory 

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 need only to remember the application name, section name, and setting name.

Tip 

Windows Vista 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, Vista doesn’t allow you to edit some parts of the Registry without elevated privileges. Fortunately, the part of the Registry used by SaveSetting is accessible to normal users, so you don’t need elevated privileges to use SaveSetting, GetSetting, 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("ImageOrganizer", "Config", "CurrentDirectory", "<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 ImageOrganizer application’s Config section. It loops through the results, displaying the setting names and values.

  Dim settings As String(,) = GetAllSettings("ImageOrganizer", "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, 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 ImageOrganizer/Config/CurrentDirectory setting. DeleteSetting("ImageOrganizer", "Config", "CurrentDirectory") ' Remove the ImageOrganizer/Config section. DeleteSetting("ImageOrganizer", "Config") ' Remove all of the ImageOrganizer application's settings. DeleteSetting("ImageOrganizer") 

Tip 

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 it 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 modifying the Registry.

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.

Open table as spreadsheet

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_CURRENT_CONFIG

Tip 

Note that some parts of the Registry are off limits to programs running as normal users in Windows Vista. 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.

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.

Open table as spreadsheet

Property or Method

Purpose

Name

Returns the key’s Registry path.

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 Software\VB and VBA Program Settings\ ImageOrganizer\Config:

My.Computer.Registry.CurrentUser.DeleteSubKey( _

“Software\VB and VBA Program Settings\

ImageOrganizer\Config”)

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. For example, the following code uses the CurrentUser RegistryKey object to delete all of the settings for the ImageOrganizer application:

My.Computer.Registry.CurrentUser.DeleteSubKeyTree( _

“Software\VB and VBA Program Settings\ImageOrganizer”)

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.

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_USER\Software\VB and VBA Program Settings\ ImageOrganizer\Config 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 ImageOrganizer application’s Config section.

  ' Open the application's Config subkey. Dim config_section As Microsoft.Win32.RegistryKey = _     My.Computer.Registry.CurrentUser.OpenSubKey( _         "Software\VB and VBA Program Settings\ImageOrganizer\Config\") ' 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 My.Computer.Registry.CurrentUser. DeleteSubKey DeleteSubKey ( _     "Software\VB and VBA Program Settings\ImageOrganizer\Config") 

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("ImageOrganizer", "Config", "CurrentDirectory", "C:\") ' Delete the application's whole Config section. DeleteSetting("ImageOrganizer", "Config") 

It is generally easier to use the native Registry methods of Visual Basic. Those methods work only with values in the HKEY_CURRENT_USER\Software\VB 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.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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