Home > Articles > Programming > .NET and Windows Programming

Reading and Writing the Registry in .NET

Scott Loban
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
XML and SQL Server 2000

Like this article? We recommend
XML and SQL Server 2000

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.

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

Hive

Description

HKEY_CLASSES_ROOT

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.

HKEY_CURRENT_USER

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

HKEY_LOCAL_MACHINE

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.

HKEY_USERS

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.

HKEY_CURRENT_CONFIG

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

HKEY_DYN_DATA

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

Type

Description

REG_DWORD

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.

REG_BINARY

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

REG_SZ

Stores information as a plain text string.

REG_MULTI_SZ

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

REG_EXPAND_SZ

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


  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jennifer  BortelWin FREE iPhone Developer Books and Videos- Introducing @InformIT Giveaways
By Jennifer Bortel on February 5, 2010 No Comments

Apples’s recent iPad announcement made our hearts flutter so we couldn’t resist making an announcement of our own!

Today marks the first ever @InformIT Giveaway!

We’ll regularly post a video like this one profiling spectacular prizes we’re giving away—from books and videos to T-shirts and other exciting stuff. Check out the video below to see the giveaways for today, and then scroll down for more prize details and instructions on how to win them!

So Far So Good
By John Traenkenschuh on February 2, 2010 No Comments

So far, Win 7 is making a thoroughbred of what has been a plough mule laptop

Dustin Sullivan"Every OSX developer should have this book on their desk."
By Dustin Sullivan on February 1, 2010 No Comments

That was the sentence Mike Riley ended his recent Dr Dobb's CodeTalk review of Cocoa Programming Developer's Handbook with.

See All Related Blogs

Informit Network