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


/ Published in: VB.NET

list all the files in a directory


Copy this code and paste it in your HTML
  1. Private Function DirToList(ByVal sFolder As String, Optional ByVal sFilter As String = "*.*", Optional ByVal bRecurvise As Boolean = True) As List(Of String)
  2. 'résultat sous forme de collection de chaîne
  3. Dim ListResult As New List(Of String)
  4. 'récupère le tableau
  5. Dim asDir() As String = DirToArray(sFolder, sFilter, bRecurvise)
  6. 'intègre dans la liste
  7. If Not (asDir Is Nothing) Then
  8. For Each s As String In asDir
  9. ListResult.Add(s)
  10. Next
  11. End If
  12. Return ListResult
  13. End Function
  14. Private Function DirToArray(ByVal sFolder As String, Optional ByVal sFilter As String = "*.*", Optional ByVal bRecurvise As Boolean = True) As String()
  15. 'ajoute le dernier "\"
  16. If Not (sFolder(sFolder.Length - 1) = "\"c) Then sFolder &= "\"
  17. 'le dossier doit existe
  18. If Not (System.IO.Directory.Exists(sFolder)) Then Return Nothing
  19. 'récupère le tableau depuis class framework
  20. If bRecurvise Then
  21. Return System.IO.Directory.GetFiles(sFolder, sFilter, IO.SearchOption.AllDirectories)
  22. Else
  23. Return System.IO.Directory.GetFiles(sFolder, sFilter, IO.SearchOption.TopDirectoryOnly)
  24. End If
  25. End Function
  26.  
  27. 'EXEMPLE
  28. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  29. Dim PhpFiles As List(Of String) = DirToList("C:\Program Files\wamp\www", "*.php", True)
  30. Dim sPluriel As String = String.Empty
  31. If (PhpFiles.Count > 1) Then sPluriel = "s"
  32. Console.WriteLine(String.Format("{0} fichier{1} trouvé{1}", PhpFiles.Count, sPluriel))
  33. For Each s As String In PhpFiles
  34. Console.WriteLine(s)
  35. Next
  36. End Sub

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.