Search code examples
c#.netregistry

Accessing all the keys under classesroot from registry with c#


I have a program which i try is to read all the keys under classesroot but when i do it with GetSubKeyNames() many keys are skipped i only get the key which contains another sub key on it.

RegistryKey key = Registry.ClassesRoot;
foreach (string tempKeyName in key.GetSubKeyNames())
{
     MessageBox.Show(tempKeyName);
 }

enter image description here


Solution

  • This code shows all the registry keys under HKEY_CLASSES_ROOT. If it doesn't work for you, edit your question telling us Windows version and maybe if you are under a corporate network.

    RegistryKey rk = Registry.ClassesRoot;
    // Print out the keys.
    PrintKeys(rk);
    

    PrintKeys:

    static void PrintKeys(RegistryKey rkey)
    {
    
        // Retrieve all the subkeys for the specified key.
        String[] names = rkey.GetSubKeyNames();
    
        Console.WriteLine("Subkeys of " + rkey.Name);
        Console.WriteLine("-----------------------------------------------");
    
        // Print the contents of the array to the console.
        foreach (String s in names)
        {
            Console.WriteLine(s);
        }
    }
    

    Source:MSDN