Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual Basic
 How to: Get the Collection of Files...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Basic Language Concepts 
How to: Get the Collection of Files in a Directory in Visual Basic 

The My.Computer.FileSystem.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.

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

    This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in File system - Processing Drives, Folders, and Files. For more information, see How to: Insert Snippets Into Your Code (Visual Basic).

Robust Programming

The following conditions may cause an exception:

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker