OpenSubKey not working for registry value I need

I have SQL Server installed.

In the registry, the MSSQLServer key in * HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft * looks like this: enter image description here

All of the following lines of code return values ​​from the registry:

    var mainKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("MSSQLServer");
    var subKey1 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("MSSQLServer").OpenSubKey("Client");
    var subKey2 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("MSSQLServer").OpenSubKey("MSSQLServer").OpenSubKey("CurrentVersion");

However, this does not happen:

var subKey3 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("MSSQLServer").OpenSubKey("Setup");

The “settings” look the same as the other keys. Any thoughts why this command returns null?

+5
source share
1 answer

Your application is 32-bit and is a 64-bit registry key. 32-bit and 64-bit applications have different registry views. In regedit, the key that you get in the code is HKLM \ Software \ Wow6432Node \ MSSQLServer, not HKLM \ Software \ MSSQLServer (in your picture).

, API 32- 64- .

: AnyCPU x64 P/Invoke API. , P/Invoke MSDN.

, OpenSubKey, .

. . .NET 4, OpenBaseKey (MSDN) 64- 32- .

+10

All Articles