Home > Articles > Home & Office Computing > Microsoft Windows Desktop

Reading and Writing the Registry in .NET

  • Print
  • + Share This
Applications have long used the Windows Registry as a data repository for storing information needed at runtime. In the Microsoft .NET Framework, the Registry class provides complete access to the Windows registry. Scott Loban introduces the classes and C# syntax used to store and retrieve information from the Windows Registry.
Like this article? We recommend

Like this article? We recommend

This article introduces the classes and syntax used to store and retrieve information from the Windows Registry. Before proceeding, please note that a damaged Registry can make applications—or even the entire computer system—unstable or completely unusable. Before attempting to programmatically read or write the Registry, back up the system state, ensuring that you have a way to restore an unbootable system.

Windows Registry

The Windows Registry is a data repository that exists on each computer in Windows 95 and later systems. Both the system and application programs use this repository to store information needed at runtime. For example, the system stores file associations (the applications or command lines associated with each known file extension) in the Registry. Applications often use the Registry to store information such as users' option settings and data file pathnames.

The system reads the Registry into memory at bootup. This memory image then serves as a working copy for the system. When the system is shut down, it persists the current Registry to disk. The Registry is stored in several files. The location and organization of these files depends on the version of Windows. For example, in Windows 2000, the Registry is stored in a number of files located in the \%SystemRoot%\System32\Config folder, whereas in Windows 95, the Registry is contained in user.dat and system.dat, which are hidden system files in the Windows folder. In all cases, these files must not be edited directly. Changes to the Registry can be made only programmatically or by using a special Registry editor application.

Structure of the Registry

The Registry has a hierarchical structure, like the folders on a disk volume. The root is not used directly, but instead holds six top-level branches, or hives. The hives are HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_CONFIG, and HKEY_DYN_DATA. These hives are described in Table 1.

Table 1—Registry Hives




The system information store that allows application software to run. This hive contains the mappings between ProgID and ClassID used by COM, as well as file associations and program launching information.


This refers to (like a symbolic link) the section in the HKEY_USERS hive relevant to the currently logged on user.


This is the core system information store. It contains system device information under its HARDWARE key, basic system information under the SYSTEM key, and application-specific information under the SOFTWARE key.


This hive contains a key for each user with an account on the machine, as well as a default user. Users' preferences and UI settings are stored here.


This is like a symbolic link to the section in HKEY_LOCAL_MACHINE\HARDWARE for the current hardware profile.


This hive may or may not be present. If it is, then it contains information related to the current plug-and-play devices on the machine.

Each hive contains a number of keys (see Figure 1). Registry keys are the basic data structure for the Registry. A Registry key acts like a subfolder, holding additional levels of subkeys, as well as values—name/value pairs that are used to hold the actual information.

Figure 1Figure 1 The organization of the Registry.

Registry Data Types

The data that is stored in a Registry value can be represented by one of a number of data types. The most common are number, text string, and direct binary data. These are implemented through five data types: REG_DWORD, REG_BINARY, REG_SZ, REG_MULTI_SZ, or REG_EXPAND_SZ. These data types are described further in Table 2.

Table 2—Registry Data Types




A double word (four-byte) number. Stores numerical data and is used for Boolean values, with 0 being false (meaning "no" or "off," for example), and 1 being true.


Stores binary data. These values appear in hexadecimal format when viewed in a Registry editor.


Stores information as a plain text string.


Stores multiple null-terminated strings. Used to store lists and arrays.


Stores an environment string variable, such as "%SystemRoot%".

  • + Share This
  • 🔖 Save To Your Account