Windows Registry

The Windows registry provides a centralized repository where the operating system and applications can store information. The registry is structured hierarchically, with the registry key being the base unit of organization. Registry keys can hold both typed data and subkeys.

Given the changes implemented with .NET, including self-describing assemblies, configuration files, and isolated storage, the importance of the registry is diminished. However, the registry is still a useful service, and the .NET Framework provides direct access to it through a simple set of classes in the Microsoft.Win32 namespace.

The Registry Class

Despite having a hierarchical structure, the registry doesn’t derive from a single root key. Microsoft.Win32.Registry is a utility class that contains seven static read-only RegistryKey fields (discussed next); these fields represent the primary entry points into the registry.

The registry classes don’t support direct random access to named registry keys; entry points must be obtained through the Registry class. The members of Microsoft.Win32.RegistryKey are used to navigate to the required registry key.

Table A-6 lists the seven static fields, indicates the area they map to in the registry, and summarizes the information contained in that area.

Table A-6. Static RegistryKey Fields of the Registry Class

Field

Description

ClassesRoot

Maps to HKEY_CLASSES_ROOT. Contains information about extension to application mappings and OLE objects.

CurrentConfig

Maps to HKEY_CURENT_CONFIG. Contains current non-user-specific hardware configuration information.

CurrentUser

Maps to HKEY_CURRENT_USER. Contains information about current user preferences.

DynData

Maps to HKEY_DYN_DATA. Contains dynamic registry data.

LocalMachine

Maps to HKEY_LOCAL_MACHINE. Contains local machine configuration data.

PerformanceData

Maps to HKEY_PERFORMANCE_DATA. Contains performance information.

Users

Maps to HKEY_USERS. Contains information about user accounts and default user information.

The RegistryKey Class

The RegistryKey class represents a key node within the Registry hierarchy. Table A-7 details the members of the RegistryKey class that provide access to information about a particular key as well as the values and child keys it contains. All get, set, and delete methods are case preserving but not case sensitive with regard to the registry key and value names.

Table A-7. Members of the RegistryKey Class

Member

Description

Properties

 

Name

Gets the name of the RegistryKey.

SubkeyCount

Gets the number of subkeys in the RegistryKey.

ValueCount

Gets the number of values in the RegistryKey.

Methods

 

Close()

Closes an open RegistryKey and flushes any changes to disk.

CreateSubKey()

Creates a new subkey with the specified name and returns a RegistryKey instance. If the subkey already exists, the existing subkey is returned.

DeleteSubKey()

Deletes the specified empty subkey.

DeleteSubKeyTree()

Deletes the subkey and all child subkeys recursively.

DeleteValue()

Deletes the specified value from the RegistryKey.

Flush()

Causes the registry write buffers to be flushed.

GetSubKeyNames()

Gets a String array containing the names of all subkeys contained in the key.

GetValue()

Gets the contained value with the specified name. The value is returned as an Object that must be cast to the correct data type. Details of the types returned are included in the .NET documentation.

GetValueNames()

Gets a String array containing the names of all values contained in the RegistryKey.

OpenSubKey()

Returns an instance of RegistryKey representing the named subkey; null is returned if the subkey doesn’t exist. An overload to this method allows the programmer to request write access to the opened key.

SetValue()

Creates or updates the specified value. Despite the fact that the registry supports different data types, it isn’t possible to choose what type will be used to store the value; the most appropriate type is chosen automatically based on the data provided.

Remote Registry Access

The registry of a remote machine can be accessed using the static RegistryKey.OpenRemoteBaseKey and providing two arguments:

  • A value from the Microsoft.Win32.RegistryHive enumeration that identifies which root key to open. The valid values map to the static fields of the Registry class discussed earlier; they are ClassesRoot, CurrentConfig, CurrentUser, DynData, LocalMachine, PerformanceData, and Users.

  • A String containing the name of the machine on which the registry to be opened is located. If String.Empty is used, the registry on the local machine is opened.

OpenRemoteBaseKey returns a RegistryKey instance representing the opened key on the remote machine and can subsequently be used as described in the preceding section.

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

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