1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #Region "class"
Public Class ListerRepertoire
Private TheTreeview As TreeView
Private Delegate Function TN_Delegate(ByVal a As TreeNode) As Int32
Public Sub New(ByVal TV As TreeView)
TheTreeview = TV 'affecte la Treeview
End Sub
#Region "procedure1"
Public Sub ListerMesRepertoire(ByVal Rep As String, ByVal Parent As TreeNode)
Try
'Recupere la liste des entrees du repetoire
Dim Liste As String() = Directory.GetFileSystemEntries(Rep)
Dim Entree As String
For Each Entree In Liste 'Pour chaque entree de la liste
Dim TmpName As String = Microsoft.VisualBasic.Mid(Entree, Len(Rep) + 1) 'On decoupe proprement le nom du fichier ou repertoire
TmpName = Microsoft.VisualBasic.Replace(TmpName, "\", "") 'On enleve les caracteres parasite !!!!
If File.Exists(Entree) Then 'Si c'est un fichier
AddNode(TmpName, True, Parent) 'On Creer un element fichier dans le treeview
Else 'Si c'est un repertoire
Dim TmpNode As TreeNode 'Var tempo pour sauver le noeud du treeview
TmpNode = AddNode(TmpName, False, Parent) 'On ajoute le rep dans le treeview
ListerMesRepertoire(Entree, TmpNode) 'On Liste le contenus de ce repertoire
End If
Next Entree
Catch Ex As System.UnauthorizedAccessException 'si le répertoire est protégé
Exit Sub
Catch e As Exception 'en cas d'erreurs
MsgBox("Erreur : " & e.Message, MsgBoxStyle.Critical)
End Try
End Sub
#End Region
#Region "procedure2"
Private Function AddNode(ByVal StrNom As String, ByVal BlFile As Boolean, ByVal Parent As TreeNode) As TreeNode
Try
Dim TmpNode As TreeNode 'Noeud Pour les manipulation dans la function
If Not Parent Is Nothing Then ' Si Parent n'Est pas a nothing
'TmpNode = Parent.Nodes.Add(Str_Nom) 'On cree un element fils
Dim no As New TreeNode
no.Text = StrNom
With TheTreeview
.Invoke(New TN_Delegate(AddressOf Parent.Nodes.Add), New Object() {no})
End With
TmpNode = no
Else ' Si Parent Est a nothing c sans doute le premier element du treeview ou une racine
'TmpNode = TreeView1.Nodes.Add(Str_Nom)
Dim no As New TreeNode
no.Text = StrNom
With TheTreeview
.Invoke(New TN_Delegate(AddressOf TheTreeview.Nodes.Add), New Object() {no})
End With
TmpNode = no
End If
Application.DoEvents() 'histoire de faire beau ^^ Non je pense que ca limitera le risque d'erreur
Return TmpNode
Catch ex As Exception
Return Nothing
MsgBox(ex.ToString)
End Try
End Function
#End Region
End Class
#End Region |
Partager