Hola foro
Tengo una carpeta en la sgte. forma:
\Batches
- Batch 1
- Batch 2
- Batch 3
- Batch 10
- Batch 20
- Batch 21
Dicha carpeta la visualizo en un TreeView, pero este al agregar las carpetas
las agrega de esta forma:
\Batches
- Batch 1
- Batch 10
- Batch 2
- Batch 20
- Batch 21
- Batch 3
Quiero que el TreeView visualize las carpetas en order numerico. He probado
implementando la clase IComparer, pero no me resulta.
Alguien me puede dar una mano?
Gracias.
PS. Este es mi codigo:
Private Sub frmImageList_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim rootPath As String = "C:\Documents and Settings\Administrator\Desktop\"
Dim rootFolder As String = "Batches"
treeView.Nodes.Add(rootFolder)
populateTreeView(rootPath & rootFolder, treeView.Nodes(0))
End Sub
Private Sub populateTreeView(ByVal folderPath As String, ByVal parentNode As
TreeNode)
' populate current node with subfolders
Try
Dim myComparer As folderComparer = New folderComparer
' get all subfolders
Dim folderArray As String() = Directory.GetDirectories(folderPath)
Array.Sort(folderArray, myComparer)
' check if it has at least one subfolder
If (folderArray.Length <> 0) Then
Dim currentFolder As String
For Each currentFolder In folderArray
Dim folderName = Directory.CreateDirectory(currentFolder).Name
' creae a new node for current folder
Dim myNode = New TreeNode(folderName)
' add current folder node to parent node
parentNode.Nodes.Add(myNode)
' populate recursively every subfolder
populateTreeView(currentFolder, myNode)
Next
End If
Catch unAuthorizedEx As UnauthorizedAccessException
parentNode.Nodes.Add("Access to this folder is denied!")
End Try
End Sub
End Class
Public Class folderComparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements IComparer.Compare
Dim strX As String = x
Dim strY As String = y
Dim idx As Integer = strX.LastIndexOf("\") + 1
strX = strX.Substring(idx, strX.Length() - idx)
strY = strY.Substring(idx, strY.Length() - idx)
Return strX.CompareTo(strY)
End Function
Leer las respuestas