[英]vb.net registry - get rid of full path from childnodes
這可能是一個菜鳥問題,無論如何現在都已經很晚了,但是我今晚想解決這個小問題。 問題在於TreeView為每個注冊表項提供完整路徑。
例如: HKEY_CURRENT_USER \\ Control Panel \\ Desktop \\ Colors如果有很多子節點,這很煩人...它應該像原始RegEdit一樣給Control Panel- > Desktop- > Colors 。
' 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
嘗試:
Dim vChildNode As New TreeNode(vSubKey.Name.Split("\"C).Last())
或不使用Linq:
Dim PathElements as string() = vSubKey.Name.Split("\"C)
Dim vChildNode As New TreeNode(PathElements(PathElements.Length-1))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.