/ Published in: VB.NET
list all the files in a directory
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
Private Function DirToList(ByVal sFolder As String, Optional ByVal sFilter As String = "*.*", Optional ByVal bRecurvise As Boolean = True) As List(Of String) 'résultat sous forme de collection de chaîne Dim ListResult As New List(Of String) 'récupère le tableau Dim asDir() As String = DirToArray(sFolder, sFilter, bRecurvise) 'intègre dans la liste If Not (asDir Is Nothing) Then For Each s As String In asDir ListResult.Add(s) Next End If Return ListResult End Function Private Function DirToArray(ByVal sFolder As String, Optional ByVal sFilter As String = "*.*", Optional ByVal bRecurvise As Boolean = True) As String() 'ajoute le dernier "\" If Not (sFolder(sFolder.Length - 1) = "\"c) Then sFolder &= "\" 'le dossier doit existe If Not (System.IO.Directory.Exists(sFolder)) Then Return Nothing 'récupère le tableau depuis class framework If bRecurvise Then Return System.IO.Directory.GetFiles(sFolder, sFilter, IO.SearchOption.AllDirectories) Else Return System.IO.Directory.GetFiles(sFolder, sFilter, IO.SearchOption.TopDirectoryOnly) End If End Function 'EXEMPLE Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim PhpFiles As List(Of String) = DirToList("C:\Program Files\wamp\www", "*.php", True) Dim sPluriel As String = String.Empty If (PhpFiles.Count > 1) Then sPluriel = "s" For Each s As String In PhpFiles Console.WriteLine(s) Next End Sub