这可能是一个菜鸟问题,反正这里已经很晚了,但我想今晚解决这个小问题。问题是 TreeView 为每个注册表项提供了完整路径。
例如:HKEY_CURRENT_USER\Control Panel\Desktop\Colors如果有很多子节点,这很烦人......它应该像原来的 RegEdit 一样提供控制面板 -> 桌面 -> 颜色。
' Recursive method which creates nodes and all child nodes for vParentNode
Private Function CreateNodes(ByVal vParentNode As TreeNode, ByVal vRegKey As RegistryKey) As TreeNode
For Each vSubKeyName As String In vRegKey.GetSubKeyNames()
Try
' Open subkey and create a childnode with subkeys name on it
' Then create childnodes for childnode
Dim vSubKey As RegistryKey = vRegKey.OpenSubKey(vSubKeyName, False, Security.AccessControl.RegistryRights.ReadKey)
Dim vChildNode As New TreeNode(vSubKey.Name)
vChildNode = CreateNodes(vChildNode, vSubKey)
vParentNode.Nodes.Add(vChildNode)
Catch ex As SecurityException
' Lots of security exceptions will be thrown if user is not admin or doesnt have access for whole registry
Catch ex As Exception
End Try
Next
Return vParentNode
End Function
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Select Case ComboBox1.Text
Case "HKEY_CURRENT_USER"
' Get registrykey for CurrentUser
Dim vRegKeyCurrentUser As RegistryKey = Registry.CurrentUser
' Create TreeNode and get its child nodes in CreateNodes method
Dim vParentNode As New TreeNode(vRegKeyCurrentUser.Name)
vParentNode = CreateNodes(vParentNode, vRegKeyCurrentUser)
' Show the nodes on treeview
TreeView1.Nodes.Add(vParentNode)
End Select
End Sub