Search code examples
c#registry

C# Cannot read key valuenames


I'm trying to read all subkey on registry in C# It's works well for native subkey, but when I create my own key, I cannot read it.

string all = "-start" + Environment.NewLine;
string registryKey2 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VisualEffects\AnimateMinMax";
RegistryKey key2 = Registry.LocalMachine.OpenSubKey(registryKey2, true);

foreach (string valueName in key2.GetValueNames())
{
    all += valueName + Environment.NewLine;
}
all += "-End" + Environment.NewLine;
MessageBox.Show(all);

enter image description here enter image description here

"Test" Dword key is missing

Do you have some tips for that ?

Best Regards


Solution

  • You are looking at the wrong registry key section. Your application is compiled as 32-bit, but your operating system is 64-bit. In this case Windows automatically redirects to WOW6432Node of the registry, where the key does not exists.

    Solutions:

    1) In project settings, change the target CPU to x64.

    2) Use RegistryView.Registry64 flag to open the 64-bit registry view.

    var localMachine64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
    var key2 = localMachine64.OpenSubKey(registryKey2, true);
    // rest of the code