Hi @Mansour_Dalir ,
Please check if the following code helps.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim MyArray As String() = {"D:\Folder1\File1.pdf", "D:\Folder1\File2.pdf", "D:\Folder2"}
For Each path As String In MyArray
AddPathToTreeView(path, TreeView1.Nodes)
Next
End Sub
Private Sub AddPathToTreeView(path As String, nodes As TreeNodeCollection)
Dim pathSegments As String() = path.Split("\"c)
Dim currentNode As TreeNodeCollection = nodes
For Each segment As String In pathSegments
Dim existingNode As TreeNode = currentNode.Cast(Of TreeNode)().FirstOrDefault(Function(node) node.Text = segment)
If existingNode Is Nothing Then
Dim newNode As TreeNode = New TreeNode(segment)
currentNode.Add(newNode)
currentNode = newNode.Nodes
Else
currentNode = existingNode.Nodes
End If
Next
End Sub
Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.