Doug Robinson Doug Robinson - 5 months ago 97
Vb.net Question

Cannot read value of Registry Key - VB.NET - HKLM

I'm trying to read the value of a string, 'Connection', under the registry key


HKEY_Local_Machine\Software\Trebuchet\ServerSetup\Business Process
Service


In VB.NET, i'm attempting to read this key using the following code:

Private Function ReadRegistry()
Dim KeyValue As String = ""
Dim regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Trebuchet\ServerSetup\Business Process Service", False)
If regkey IsNot Nothing Then KeyValue = CStr(regkey.GetValue("Connection"))

Return KeyValue
End Function


However, when attempting to check the registry, I receive a null for regkey. I've verified that the value is within that key, and have even replaced the text within the OpenSubKey call to be an exact copy of the key name as retrieved from RegEdit, but it seems the VB app can't read it for some reason.

Am I missing something?

Answer

My guess is you're developing 32-bit application on a 64-bit OS. In this case, the shared (static in C#) members of the Registry class like LocalMachine won't be a fit because they're looking in the 32-bit version of the registry. You need to open the base key in the registry, specifying that you want 64-bit version, explicitly. So your code might look like this:

Private Function ReadRegistry()
    Dim KeyValue As String = ""
    Dim baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
    Dim regkey = baseKey.OpenSubKey("SOFTWARE\Trebuchet\ServerSetup\Business Process Service")
    If regkey IsNot Nothing Then KeyValue = CStr(regkey.GetValue("CLSID"))

    Return KeyValue
End Function