Question

I like to enumerate the Registrykeys. I am so far:

  Dim key As Microsoft.Win32.RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\")
    For Each subkey In key.GetSubKeyNames
        ListBox1.Items.Add(subkey.ToString)
    Next

But I like to specify the Registry hive in a Function parameter like this:

    Private Function listregistry(ByVal hive As RegistryHive, ByVal path As String)
    Dim key As Microsoft.Win32.RegistryKey = My.Computer.Registry.hive.OpenSubKey(path)
    For Each subkey In key.GetSubKeyNames
        ListBox1.Items.Add(subkey.ToString)
    Next
End Function

That gives me this error:

'hive' is not a member of 'Microsoft.VisualBasic.MyServices.RegistryProxy'

What can I do to fix this?

Well I was able to get it to work, but this is a bit sloppy, how can I optimize this:

 Private Sub ListRegistryKeys(ByVal RegistryHive As String, ByVal RegistryPath As String)
    Select Case RegistryHive
        Case "HKEY_LOCAL_MACHINE"
            Dim key As Microsoft.Win32.RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey(RegistryPath)
            For Each subkey In key.GetSubKeyNames
                ListBox1.Items.Add(subkey.ToString)
            Next
        Case "HKEY_CURRENT_USER"
            Dim key As Microsoft.Win32.RegistryKey = My.Computer.Registry.CurrentUser.OpenSubKey(RegistryPath)
            For Each subkey In key.GetSubKeyNames
                ListBox1.Items.Add(subkey.ToString)
            Next
        Case "HKEY_CLASSES_ROOT"
            Dim key As Microsoft.Win32.RegistryKey = My.Computer.Registry.ClassesRoot.OpenSubKey(RegistryPath)
            For Each subkey In key.GetSubKeyNames
                ListBox1.Items.Add(subkey.ToString)
            Next
        Case "HKEY_CURRENT_CONFIG"
            Dim key As Microsoft.Win32.RegistryKey = My.Computer.Registry.CurrentConfig.OpenSubKey(RegistryPath)
            For Each subkey In key.GetSubKeyNames
                ListBox1.Items.Add(subkey.ToString)
            Next
        Case "HKEY_USERS"
            Dim key As Microsoft.Win32.RegistryKey = My.Computer.Registry.Users.OpenSubKey(RegistryPath)
            For Each subkey In key.GetSubKeyNames
                ListBox1.Items.Add(subkey.ToString)
            Next
    End Select
End Sub

No correct solution

OTHER TIPS

Try this:

Private Function listregistry(ByVal hive As Microsoft.Win32.RegistryHive, ByVal path As String)
    Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.RegistryKey.OpenBaseKey(hive, Microsoft.Win32.RegistryView.Default).OpenSubKey(path)

    For Each subkey In key.GetSubKeyNames
        ListBox1.Items.Add(subkey.ToString)
    Next
End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top