Results 1 to 4 of 4

Thread: Read from Registry to ListView1

  1. #1

    Thread Starter
    Junior Member scot-65's Avatar
    Join Date
    Mar 2013
    Posts
    16

    Read from Registry to ListView1

    I am trying to populate a 2-column ListView using the registry key and data. I am able to "build" the listview and save to the registry. The first column of the list view is the registry subkey and the second column is the data.

    This project sat idle for over 13 years and I am basically self-taught. Things have changed.

    List View owner draw is true.
    rootB is Public const = "HKCU\software\my sorter."
    Registry is read from at this time.
    There are currently 2 keys (profiles) where each key has 2 subkeys with assigned values (test mode).
    The profile argument is the last saved profile.
    There are stray comments that have not been removed - my apologies.
    Program has issues at the line "GetSubKeyNames()".

    Code:
       Private Sub GetREG(ByRef profile As String)
          'If Registry.GetValue(rootB + "\" + profile, "", Nothing) <> Nothing Then
    
          'clear the ListView box
          ListView1.Items.Clear()
    
          'rebuild the ListView box
          MessageBox.Show("Profile in argument: " + profile) '===OK.
    
          Dim RegKey As RegistryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\My Sorter\" + profile)
          For Each Subkey As String In RegKey.GetSubKeyNames()
             'For Each Subkey In Subkeys
             Dim tempkey As RegistryKey = RegKey.OpenSubKey(Subkey)
             MessageBox.Show("subkey name: ", Subkey)
             'ListView1.Items.Add(Subkey, Registry.GetValue(rootB + "\" + profile, Subkey, ""))
    
             Dim listItem As New ListViewItem(tempkey.ToString)
             listItem.SubItems.Add(Registry.GetValue(rootB + "\" + profile, tempkey.ToString, ""))
             ListView1.Items.Add(listItem)
    
          Next
          RegKey.Close()
    
          'Else
          'MessageBox.Show("No GetValue: " + profile)
          'End If
    
       End Sub
    Nested parenthesis is nothing new to me and I will be able to do so if given the chance.

  2. #2
    Fanatic Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    629

    Re: Read from Registry to ListView1

    which version of .net framework are you using with this?


    EDIT: This is .Net 8 and is just dumping to a listbox

    Code:
            Try
                Using regKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Helium")
                    If regKey Is Nothing Then
                        ListBox1.Items.Add("nothing found")
                        Return
                    End If
    
                    For Each valName As String In regKey.GetValueNames()
                        Dim valObj As Object = regKey.GetValue(valName)
                        Dim kind As Microsoft.Win32.RegistryValueKind = regKey.GetValueKind(valName)
                        Dim displayName As String = If(String.IsNullOrEmpty(valName), "(Default)", valName)
                        Dim displayValue As String
    
                        If valObj Is Nothing Then
                            displayValue = "(value not set)"
                        ElseIf TypeOf valObj Is Byte() Then
                            displayValue = BitConverter.ToString(DirectCast(valObj, Byte()))
                        Else
                            displayValue = valObj.ToString()
                        End If
    
                        ListBox1.Items.Add($"{displayName} = {displayValue} ({kind.ToString()})")
                    Next
                End Using
            Catch ex As Exception
                ListBox1.Items.Add($"Error reading registry: {ex.Message}")
            End Try
    Last edited by jdelano; Today at 09:52 AM.

  3. #3

    Thread Starter
    Junior Member scot-65's Avatar
    Join Date
    Mar 2013
    Posts
    16

    Re: Read from Registry to ListView1

    @jdelano,

    .NET Framework 4.8
    Visual Studio 2026 18.6.2 community edition.

  4. #4

    Thread Starter
    Junior Member scot-65's Avatar
    Join Date
    Mar 2013
    Posts
    16

    Re: Read from Registry to ListView1

    @jdelano,

    I just noticed you made reference to a ListBox and not a ListView.
    Anyhow, thanks for the right direction - it was not GetSubKeyNames but rather GetValueNames.

    Almost finished code (more error checking to do):
    [CODE]
    Private Sub GetREG(ByRef profile As String)
    ListView1.OwnerDraw = False
    'clear the ListView box
    ListView1.Items.Clear()
    'rebuild the ListView box
    Dim RegKey As RegistryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\My Sorter\" + profile)
    If RegKey Is Nothing Then
    MessageBox.Show("There is no available profile.", "Profile is empty", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Else
    For Each Subkey As String In RegKey.GetValueNames()
    Dim val As String = Registry.GetValue(rootB + "\" + profile, Subkey, "")

    Console.WriteLine("Profile: {0} subkey: {1} value: {2}", profile, Subkey, val)

    Dim listItem As New ListViewItem(Subkey)
    listItem.SubItems.Add(val)
    ListView1.Items.Add(listItem)
    Next
    End If
    RegKey.Close()
    End Sub
    [\CODE]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width