Search code examples
c#.netwindowsregistryregistrykey

OpenSubKey() Registry key's “Absolute Path”?


Using Microsoft.Win32.RegistryKey C# functions which require a registry path, like OpenSubKey(), using a path like

@"SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"

generates an error stating “Absolute path information is required.”

What is the syntax to create the absolute path required?


Solution

  • The registry has a couple of root keys and all subkeys are relative to one of these.

    In order to use the OpenSubKey method, you must have an instance of the RegistryKey method. To get an instance of RegistryKey, use one of the static members of the Registry class.

    If for example you want the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet as seen in Regedit you would have to start with Registry.LocalMachine.

    RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet");
    ... = rk.GetValue(...);
    

    If you already have a key, yourkey.Name is the path of the key.