Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenSubKey not working for Registry value I need

Tags:

c#

I have SQL Server installed.

In the registry, the key MSSQLServer at *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 one does not:

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

"Setup" looks to be the same type of value as the other keys. Any thoughts why this command returns null?

like image 333
ray Avatar asked Jun 06 '12 17:06

ray


People also ask

How do I use the opensubkey method?

In order to use the OpenSubKey method, you must have an instance of the RegistryKey class. To get an instance of RegistryKey, use one of the static members of the Registry class. Security. for the ability to read the specified registry key. for the ability to access the specified registry key if it is a remote key.

How do I open a subkey in registrykey?

In order to use the OpenSubKey method, you must have an instance of the RegistryKey class. To get an instance of RegistryKey, use one of the static members of the Registry class. Retrieves the value associated with the specified name. Creates a new subkey or opens an existing subkey.

Why is the name of my registry key null?

name is null. The RegistryKey is closed (closed keys cannot be accessed). The user does not have the permissions required to access the registry key in the specified mode. The following code example creates a test key and uses the OpenSubKey method to open it, demonstrating both overloads of the method.

What happens if the specified subkey cannot be found?

If the specified subkey cannot be found, then null is returned. In order to use the OpenSubKey method, you must have an instance of RegistryKey. To get an instance of RegistryKey, use the one of the static members of the Registry class. Retrieves the specified subkey for read or read/write access.


1 Answers

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

Unfortunately, the managed API for accessing registry keys does not allow a 32-bit application to get a 64-bit view of the registry.

There are two options: Change your application to target AnyCPU or x64 or P/Invoke the unmanaged API. This has an example of how to do the P/Invoke and the MSDN documentation.

BTW, you can pass a path to the OpenSubKey method instead of chaining them together like that.

Update: I was not quite correct above. As of .NET 4, the OpenBaseKey method (MSDN) can be used to get a 64-bit view in a 32-bit application.

like image 184
Mike Zboray Avatar answered Nov 06 '22 03:11

Mike Zboray