Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast way to read all registry values

Tags:

c#

.net

registry

I'm writing a utility that needs to create a list of all registry values in HKCR. I am doing this using a recursive function:

var list = new Dictionary<string, string>();
Read(Registry.ClassesRoot, list);

static void Read(RegistryKey root, IDictionary<string, string> values)
{
    foreach (var child in root.GetSubKeyNames())
    {
        using (var childKey = root.OpenSubKey(child))
        {
            Read(childKey, values);
        }
    }            

    foreach (var value in root.GetValueNames())
    {
        values.Add(string.Format("{0}\\{1}", root, value), (root.GetValue(value) ?? "").ToString());
    }
}

This works fine, but it takes a good chunk of time (20 seconds on my PC). Is there a faster way to do this, or is this as good as it gets?

like image 733
Jon B Avatar asked Nov 14 '12 19:11

Jon B


People also ask

How do I see all in regedit?

In the search box on the taskbar, type regedit, then select Registry Editor (Desktop app) from the results. Right-click Start , then select Run. Type regedit in the Open: box, and then select OK.

How do I read a registry value in PowerShell?

One of the easiest ways to find registry keys and values is using the Get-ChildItem cmdlet. This uses PowerShell to get a registry value and more by enumerating items in PowerShell drives. In this case, that PowerShell drive is the HKLM drive found by running Get-PSDrive .

Why is searching the Windows registry so slow?

Because the Windows registry is a hierarchy, the way regedit is laid out makes typical searches inefficient and slow. Typically, when performing a search in regedit, you highlight the first line ('Computer') and then perform the search.

Which command displays registry entry values?

Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion. This command gets the value name and data of the ProgramFilesDir registry entry in the CurrentVersion registry subkey. The command uses the Path parameter to specify the subkey and the Name parameter to specify the value name of the entry.


1 Answers

There's a lot of information in the registry and as others have noted here - speed can be an issue. If you want a list of registry settings to display in a tree-like view for the user to browse through, a better solution might be to scan the top level entries first, then as the user navigates through the tree, so you scan for those child values/settings as the tree node is opening.

I suspect this is how Microsoft's own regedit works.

like image 73
Swomble Avatar answered Sep 24 '22 10:09

Swomble