Udostępnij za pośrednictwem


How to: Get the Collection of Files in a Directory in Visual Basic

The GetFiles method returns a read-only collection of strings representing the name of the files within a directory. You can use the wildCards parameter to specify a specific pattern. To include subdirectories in the search, set the searchType parameter to SearchOption.SearchAllSubDirectories.

An empty collection is returned if no files matching the specified pattern are found.

To list files in a directory

  • Use the GetFiles method, supplying the name and path of the directory to search. The following example returns all files in the directory and adds them to ListBox1.

    For Each foundFile As String In My.Computer.FileSystem.GetFiles(
      My.Computer.FileSystem.SpecialDirectories.MyDocuments)
    
        listBox1.Items.Add(foundFile)
    Next
    

Robust Programming

The following conditions may cause an exception:

See Also

Tasks

How to: Find Files with a Specific Pattern in Visual Basic

How to: Find Subdirectories with a Specific Pattern in Visual Basic

Reference

GetFiles