The RegistryKey class contains the core functionality for reading and writing to the Windows registry. Each RegistryKey object represents an individual key in the registry. You can use the properties of this class to find out how many values this key contains (ValueCount), how many subkeys (SubKeyCount) there are, and the fully qualified key name (Name).

To open a subkey for modification, use the overloaded version of the OpenSubKey() method — which allows you to specify the writable parameter — and set it to true. You can open subkeys that are several levels deep by separating keys with a backslash (\). In C#, the backslash must be escaped, as in mykey.OpenKey("Software\\Microsoft"). You can also use methods such as CreateSubKey() and DeleteSubKey(). In the registry, keys are logical groupings, and values are the entries used to store the actual data. You can use the GetValue(), SetValue(), and DeleteValue() methods to manipulate a named value in the current key.

Changes to the registry are propagated across the system automatically, and are flushed to disk automatically by the system. You should never need to use methods such as Flush(), unless you require absolute certainty that a registry change has been written to disk. The OpenRemoteBaseKey() method opens the registry on a remote computer, provided both machines are running the remote registry service and have remote administration enabled.

public sealed class RegistryKey : MarshalByRefObject : IDisposable ...

Get C# in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.