0

I am just trying to do a simple check if a registry location exists and the answers from other questions here are not working. Here is my code. I have a label for ON and a label for OFF that are just the entry is found or not. I have the registry entry in but not getting the labels to flip. FriendlyName is a key in 7&639dc5f&0&0000.

Private Sub CheckUSB_Serial()
If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_04E2&PID_1422&MI_00\7&639dc5f&0&0000", _
                                     "FriendlyName", Nothing) Is Nothing Then
        USB_SERIAL_ON.Visible = False
        USB_SERIAL_OFF.Visible = True
    Else
        USB_SERIAL_ON.Visible = True
        USB_SERIAL_OFF.Visible = False
    End If
End Sub
Filburt
  • 17,626
  • 12
  • 64
  • 115
Mike
  • 33
  • 8
  • 1
    You mean, you have set a breakpoint in both branches of the `If` condition in this method, the code stops in both but the Labels show no effect when their visibility changes? – Jimi Jun 16 '22 at 22:32
  • For how to read the registry see this [post](https://stackoverflow.com/a/72651487/10024425). The code is in `MSPortsNet.vb` in function `ComDBGet`. If you're reading the 64-bit registry from a 32-bit app, use [RegistryKey.OpenBaseKey](https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registrykey.openbasekey?view=net-6.0) as shown in the post. Function `ComDBSet` shows how to write to the registry. – Tu deschizi eu inchid Jun 17 '22 at 00:34

2 Answers2

2

Starting with .NET Framework 4 (I believe), RegistryKey.OpenBaseKey is available which allows one to specify a Registry View. This is particularly useful when attempting to read the 64-bit registry on a 64-bit OS when one's application is running as 32-bit. It works for both 32-bit and 64-bit operating systems. RegistryKey.OpenSubKey is used in conjunction with RegistryKey.OpenBaseKey.

According to the documentation, OpenSubKey returns null (Nothing) if the operation failed. It also raises the following exceptions:

  • ArgumentNullException
  • ObjectDisposedException
  • Security Exception

Therefore if one attempts to open a registry subkey and the value is null (Nothing), the key doesn't exist.

Try the following:

Add the following imports statement

  • Imports Microsoft.Win32

SubkeyExists:

Public Function SubkeyExists(hive As RegistryHive, subkey As String, Optional regView As RegistryView = RegistryView.Registry64) As Boolean
    'open specified registry hive with specified RegistryView
    Using rKey As RegistryKey = RegistryKey.OpenBaseKey(hive, regView)
        If rKey IsNot Nothing Then
            Using sKey As RegistryKey = rKey.OpenSubKey(subkey, False)
                If sKey IsNot Nothing Then
                    Return True
                Else
                    Return False
                End If
            End Using
        Else
            Throw New Exception($"Error (SubkeyExists) - Could Not open '{hive.ToString()}' ")
        End If
    End Using
End Function

Usage:

Dim result As Boolean = SubkeyExists(RegistryHive.LocalMachine, "SYSTEM\CurrentControlSet\Enum\USB\VID_04E2&PID_1422&MI_00\7&639dc5f&0&0000")
'Dim result As Boolean = SubkeyExists(RegistryHive.LocalMachine, "SYSTEM\CurrentControlSet\Enum\USB\VID_04E2&PID_1422&MI_00\7&639dc5f&0&0000", RegistryView.Default)

Alternatively, one could use recursion to check if each portion of the registry subkey exists (left-to-right).


To get a value from a ValueName within a specified subkey, one can use the following:

GetRegistryValue:

Public Function GetRegistryValue(hive As RegistryHive, subkey As String, valueName As String, Optional regView As RegistryView = RegistryView.Registry64) As Object
    Using rKey As RegistryKey = RegistryKey.OpenBaseKey(hive, regView)
        If rKey IsNot Nothing Then

            'open subkey
            Using sKey As RegistryKey = rKey.OpenSubKey(subkey, False)
                If sKey IsNot Nothing Then
                    'read from registry
                    'Debug.WriteLine($"'{valueName}' Data Type: {sKey.GetValueKind(valueName)}")
                    Return sKey.GetValue(valueName)
                Else
                    Throw New Exception($"Error (GetRegistryValue) - Could not open '{subkey}'")
                End If
            End Using
        Else
            Throw New Exception($"Error (GetRegistryValue) - Could Not open '{hive.ToString()}' ")
        End If
    End Using
End Function

Usage

Dim result As String = CType(GetRegistryValue(RegistryHive.LocalMachine, "SYSTEM\CurrentControlSet\Enum\USB\VID_04E2&PID_1422&MI_00\7&639dc5f&0&0000", "FriendlyName"), String)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
0

Thank you all for the help. This is the code I ended up using and does what I need it to:

Imports Microsoft.Win32

Private Sub CheckUSB_Serial()
    Dim RegVersion As Microsoft.Win32.RegistryKey
    Dim KeyValue = "SYSTEM\\CurrentControlSet\\Enum\\USB\\VID_04E2&PID_1422&MI_00\\7&639dc5f&0&0000"
    RegVersion = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(KeyValue, False)
    If RegVersion IsNot Nothing Then
        USB_SERIAL_ON.Visible = True
        USB_SERIAL_OFF.Visible = False
        RegVersion.Close()
    End If
    If RegVersion Is Nothing Then
        USB_SERIAL_ON.Visible = False
        USB_SERIAL_OFF.Visible = True
        RegVersion.Close()
    End If
End Sub
Mike
  • 33
  • 8
  • 1
    The result from `OpenSubKey` is `Disposable`, so you should be either using a `Using` block or explicitly disposing it when you're done. – Craig Jun 17 '22 at 13:57
  • Looks like the examples in [RegistryKey.OpenSubKey](https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registrykey.opensubkey?view=netframework-4.8#microsoft-win32-registrykey-opensubkey(system-string)) don't call dispose either. However, the documentation for [RegistryKey.Dispose](https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registrykey.dispose?view=netframework-4.8) states: _Call Dispose when you are finished using the RegistryKey_. – Tu deschizi eu inchid Jun 17 '22 at 17:02