2

when I try to list registry values it doesn't list all the values. Like when I'm doing:

RegistryKey regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");

foreach (string vName in regKey.GetValueNames())
{
     Console.WriteLine("Reg values: " + vName);
}

the answer I get is just these:

Reg values: CurrentVersion
Reg values: CurrentBuild
Reg values: SoftwareType
Reg values: CurrentType
Reg values: InstallDate
Reg values: RegisteredOrganization
Reg values: RegisteredOwner
Reg values: SystemRoot
Reg values: InstallationType
Reg values: EditionID
Reg values: ProductName
Reg values: CurrentBuildNumber
Reg values: BuildLab
Reg values: BuildLabEx
Reg values: BuildGUID
Reg values: CSDBuildNumber
Reg values: PathName

I only get 17 lines when (if I look in the registry) 21 lines.

What am I doing wrong? Greatful for every answer.

Nix
  • 57,072
  • 29
  • 149
  • 198
Fred Asp
  • 21
  • 2
  • 1
    Check, if you're reading valid x64 or x86 registry key http://stackoverflow.com/questions/6304275/c-reading-the-registry-productid-returns-null-in-x86-targeted-app-any-cpu-wo – Chojny Jul 14 '11 at 11:42
  • On Windows XP there is only 19 17 of them are REG_SZ 2 REG_Binary and 1 is REG_DWORD. 3 of those are not string values. In other words your method is flawed. – Security Hound Jul 14 '11 at 11:44

1 Answers1

1

This is because you're running your application as 32bit, and it is being redirected to the Wow64 node in the registry. You need to either change your application to x64/Anycpu or PInvoke the Windows Registry APIs manually and pass the KEY_WOW64_64KEY option for samDesired in RegOpenKeyEx.

Edit: As posted by a commenter if you don't want to change to AnyCPU/x64 you can pass the KEY_WOW64_64KEY parameter to the .NET functions without resorting to the Windows API as well. See C# Reading the registry: ProductID returns null in x86 targeted app. “Any CPU” works fine

Community
  • 1
  • 1
0x5f3759df
  • 2,349
  • 1
  • 20
  • 25
  • Thank you bunch. It's a little bit tricky to fix it in Visual C# express. But here is an answer to that, http://stackoverflow.com/questions/4104228/change-target-cpu-settings-in-visual-studio-2010-express – Fred Asp Jul 14 '11 at 12:07
  • You can only pass the `Registry64` value when targeting .Net 4.0. For earlier versions of the .Net framework you still need P/Invoke. – Justin Jul 14 '11 at 12:12