1
votes

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?

1
Sorry - I had left the title after starting a post about a nullreferenceexception i was receiving, but resolved that issue halfway through the post and changed the body, leaving a bad title. It has been updated. The issue i'm having now is that I'm attempting to open a registry key, and it's returning a null (And I'm not receiving an exception anymore because I've added a check to the statement that would use that value to check that it's not nothing). I'm trying to find out why that opensubkey statement is returning a null even though the registry key exists.schizoid04
Best guess: you need to do something with the spaces in your. But it's just a guess, so I'm only leaving this as a comment. To test is, open the parent key `SOFTWARE\Trebuchet\ServerSetup` and get a list the sub keys.Joel Coehoorn

1 Answers

3
votes

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