Return to Snippet

Revision: 77132
at August 23, 2019 03:28 by martinbrait


Initial Code
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"
        Console.WriteLine(String.Format("{0} fichier{1} trouvé{1}", PhpFiles.Count, sPluriel))
        For Each s As String In PhpFiles
            Console.WriteLine(s)
        Next
    End Sub

Initial URL

                                

Initial Description
list all the files in a directory

Initial Title
[vbnet] [directory] lister tous les fichiers d'un répertoire

Initial Tags
list, files, directory

Initial Language
VB.NET