Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenSubKey under HKLM\Software returning null

Tags:

c#

registry

Here's my code:

Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ADM");

The registry entry exists on the machine. key is always null.

I don't think that this is a security issue. I'm running as Administrator. (I've even explicitly ran the assembly under Administrator mode).

I'm using Visual Studio 2010 running on Windows 7 64bit.

like image 999
Ian Avatar asked Sep 07 '10 05:09

Ian


2 Answers

The problem is that I'm running 64bit and my app is compiled as 32bit.

The key being read by:

Microsoft.Win32.RegistryKey key = 
    Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE");

Is not HKLM\SOFTWARE but instead HKLM\SOFTWARE\Wow6432Node\. Compiling the application as x64 solves the problem.

like image 133
Ian Avatar answered Nov 05 '22 20:11

Ian


Try opening each registry key individually like this

Microsoft.Win32.RegistryKey key1 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE");
Microsoft.Win32.RegistryKey key2 = key1.OpenSubKey(@"ADM");

Instead of using the string @"SOFTWARE\ADM";

like image 3
Ahmed Ghonim Avatar answered Nov 05 '22 19:11

Ahmed Ghonim