List all folders in a directory

It doesn't seem to find a way to do this, google does not give me!

Please help, thanks!

+5
source share
3 answers

Try the following:

Imports System
Imports System.IO

Class Program
    Shared Sub Main()
        For Each Dir As String In Directory.GetDirectories("c:\Program Files")
            Console.WriteLine(Dir)
        Next
    End Sub
End Class

I use a method Directory.GetDirectoriesthat returns an array of strings, one for each subdirectory of the directory that I provide as a parameter to the method.

+13
source
DirectoryInfo di = new DirectoryInfo("path");

di.GetDirectories();
+4
source
di = New DirectoryInfo(path)

rgFiles = di.GetFiles("*.*", IO.SearchOption.AllDirectories)

For Each fi As FileInfo In rgFiles
    If CheckIfExist(fi.FullName.ToString.Replace("\" & fi.Name, "")) = False Then
        ListBox1.Items.Add(fi.FullName.ToString.Replace("\" & fi.Name, ""))
    End If
Next

Public Function CheckIfExist(ByRef Path As String) As Boolean
    Dim RetVal As Boolean = False

    For Each LI As String In ListBox1.Items
        If LI.ToString = Path Then
            RetVal = True
            Return RetVal
            Exit Function
        End If
    Next
    Return RetVal
End Function
0
source

All Articles