View Single Post
  #3 (permalink)  
Old 07-02-2009, 7:11 AM
grmbl's Avatar
grmbl grmbl is offline
VB.NET Forum Enthusiast
.NET Framework: .NET 3.0 (VS 2005/2008)
 
Join Date: Jun 2009
Location: Belgium
Age: 27
Posts: 32
Reputation: 12
grmbl is on a distinguished programming path ahead
Default

This is the more flexible code:

NOTE: this is not recursive!!! Not everything is shown!!!

(Uncomment imageindex/selectedimageindex if you have an imagelist set with
appropriate images and have a treeview with that imagelist set.)

Code:
Public Sub RegNode(ByVal tree As TreeView, ByVal reg As Microsoft.Win32.RegistryKey)

        Dim tn As TreeNode
        ' Add node with name of registrykey
        tn = New TreeNode(reg.Name.ToString)
        'tn.ImageIndex = 4
        'tn.SelectedImageIndex = 4

        'Get all the subkeynames and put them in an array
        Dim tns() As String = reg.GetSubKeyNames()

        'Loop through all subkeynames and their subkeys
        For Each k As String In tns
            Dim n As TreeNode
            n = New TreeNode(k)
            'n.ImageIndex = 3
            'n.SelectedImageIndex = 3
            Try
                Dim sk() As String = reg.OpenSubKey(k).GetSubKeyNames
                For Each sksk As String In sk
                    'n.Nodes.Add(sksk, sksk, 3, 3)
                    n.Nodes.Add(sksk, sksk)
                Next
            Catch ex As Exception
                'Failsafe for subkeys that are inaccessible
                'n.ImageIndex = 6
                'n.SelectedImageIndex = 6
            End Try
            tn.Nodes.Add(n)
        Next

        tree.Nodes.Add(tn)

End Sub
Usage:
Code:
RegNode(tReg, Microsoft.Win32.Registry.ClassesRoot)
Example:
Code:
Private Sub fRegistry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Add node with computername
        Dim root As TreeNode
        root = New TreeNode(My.Computer.Name)
        'root.ImageIndex = 5
        'root.SelectedImageIndex = 5

        tReg.Nodes.Add(root)

        RegNode(tReg, Microsoft.Win32.Registry.ClassesRoot)
        RegNode(tReg, Microsoft.Win32.Registry.CurrentConfig)
        RegNode(tReg, Microsoft.Win32.Registry.CurrentUser)
        RegNode(tReg, Microsoft.Win32.Registry.LocalMachine)

        ' Expand computername node
        tReg.Nodes(0).Expand()

End Sub
__________________
"The world is coming to an end... SAVE YOUR BUFFERS !"

Last edited by grmbl; 07-02-2009 at 9:30 AM.
Reply With Quote