How to: Find Files with a Specific Pattern in Visual Basic
The GetFiles method returns a read-only collection of strings representing the path names for the files. You can use the wildCards parameter to specify a specific pattern. If you would like 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.
Note
For information about returning a file list by using the DirectoryInfo class of the System.IO namespace, see GetFiles.
To find files with a specified pattern
Use the GetFiles method, supplying the name and path of the directory you want to search and specifying the pattern. The following example returns all files with the extension .dll in the directory and adds them to ListBox1.
For Each foundFile As String In My.Computer.FileSystem.GetFiles( My.Computer.FileSystem.SpecialDirectories.MyDocuments, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.dll") Listbox1.Items.Add(foundFile) Next
Security
The following conditions may cause an exception:
The path is not valid for one of the following reasons: it is a zero-length string, it contains only white space, it contains invalid characters, or it is a device path (starts with \\.\) (ArgumentException).
The path is not valid because it is Nothing (ArgumentNullException).
directory does not exist (DirectoryNotFoundException).
directory points to an existing file (IOException).
The path exceeds the system-defined maximum length (PathTooLongException).
A file or folder name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
The user lacks necessary permissions to view the path (SecurityException).
The user lacks necessary permissions (UnauthorizedAccessException).
See Also
Tasks
How to: Find Subdirectories with a Specific Pattern in Visual Basic
Troubleshooting: Reading from and Writing to Text Files (Visual Basic)
How to: Get the Collection of Files in a Directory in Visual Basic