2013-11-01 2 views
0

Это, вероятно, вопрос о нобе, здесь все равно поздно, но я хочу исправить эту маленькую вещь сегодня вечером. Проблема в том, что TreeView предоставляет полные пути для каждого отдельного раздела.vb.net registry - избавиться от полного пути от childnodes

Например: HKEY_CURRENT_USER \ Control Panel \ Desktop \ Colors что раздражает, если есть много ChildNodes ... Это должно дать Control Panel -> Desktop -> Цвета как оригинальный 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 

ответ

1

Try:

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)) 
+0

Может работать, но сейчас он дает ошибку: Expression ожидается в Сплите ** (** '\\') – rip2444

+0

Извините. Попробуйте '' \ 'C' вместо '' \\' '. –

+0

Я исправил ответ –

Смежные вопросы