Search code examples
c#.netwindowsregistry

How safe is it to assume the registry hives will always be the same?


I am writing a console application that allows you to interact with the registry. The application starts off with the string path been set empty. When the user types ls, I want it to list all the registry hives (because they are currently at the top level of the local machine). However after some extensive research I cannot find a way to get all registry hives on the current machine. How safe is it to assume that these hives will always be there?

HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
HKEY_CURRENT_CONFIG

If it is not safe to think these are always there, how could I get them dynamically?

(and sorry if "hives" is not the correct term for root level subkeys, I'm quite new to dealing with the registry)


Solution

  • The "Standard Hives" are documented, so they will not change.

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms724877(v=vs.85).aspx

    To open a key, an application must supply a handle to another key in the registry that is already open. The system defines predefined keys that are always open. Predefined keys help an application navigate in the registry and make it possible to develop tools that allow a system administrator to manipulate categories of data. Applications that add data to the registry should always work within the framework of predefined keys, so administrative tools can find and use the new data.

    An application can use handles to these keys as entry points to the registry. These handles are valid for all implementations of the registry, although the use of the handles may vary from platform to platform.

    HKEY_CLASSES_ROOT is not a hive, it is a view into other hives, but is a predefined key that will always be open.

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms724836(v=vs.85).aspx

    See @Eser 's comment above that they are also in the Microsoft.Win32.RegistryHive enumeration.

    https://msdn.microsoft.com/en-us/library/ctb3kd86(v=vs.110).aspx