C
C
Chamalion2020-11-22 00:00:01
C++ / C#
Chamalion, 2020-11-22 00:00:01

How to get an array of all subkeys of a Windows registry key in C#?

So, at the moment, the functionality of my program looks like this:

Screenshot
5fb97f4164791675245145.png

Here's how I added the registry keys and their nested keys to the treeView:

treeView_registryKeys.Nodes.Add(new TreeNode("Computer"));

            RegistryKey[] regKeyArray = new RegistryKey[] { Registry.ClassesRoot,
                                                            Registry.CurrentUser,
                                                            Registry.LocalMachine,
                                                            Registry.Users,
                                                            Registry.CurrentConfig
                                                      };

            foreach (RegistryKey key in regKeyArray)
            {
                TreeNode node = new TreeNode() { Name = "node", Text = key.ToString() };
                treeView.FindNodeByName(treeView_registryKeys.Nodes[0], "Computer").Nodes.Add(node);
            }

            String[] regClassesRootKeyArray = new String[1000];
            regClassesRootKeyArray = Registry.ClassesRoot.GetSubKeyNames();

            foreach (String subKey in regClassesRootKeyArray)
            {
                TreeNode node = new TreeNode() { Name = "node", Text = subKey };
                treeView.FindNodeByName(treeView_registryKeys.Nodes[0], "HKEY_CLASSES_ROOT").Nodes.Add(node);
            }


As you can see, to get an array of subkeys of the root key (key) of the registry, you need to use the method:
Registry.[root registry key name].GetSubKeyNames()


I also want to get an array of SubSubKeys of these SubKeys (for example, subSubKeys of the SOFTWARE SubKey), and then their SubSubSubKeys, and so on until the very end of the registry nesting. However, this method only works for subkeys (root registry keys). Which method should I use in this case?

PS Actually this method works for any keys (I didn't know at the time of writing the question). It's just that if you have a String key name, then in order to get its subkeys, you must first "open" this key, that is, get it, only in the form of a RegistryKey, not a String. To do this, use the method [name_of_the_root_partition(key)].OpenSubKey(name of the String type key). This can be equated to a variable of type RegistryKey. For example, RegistryKey subKeyOpened = Registry.CurrentUser.OpenSubKey("Software");

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question