FileSystem.GetFiles Method (String, SearchOption, String[])
Returns a read-only collection of strings representing the names of files within a directory.
Namespace: Microsoft.VisualBasic.FileIO
Assembly: Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)
public static ReadOnlyCollection<string> GetFiles( string directory, SearchOption searchType, params string[] wildcards )
Parameters
- directory
- Type: System.String
Directory to be searched.
- searchType
- Type: Microsoft.VisualBasic.FileIO.SearchOption
Whether to include subfolders. Default is SearchOption.SearchTopLevelOnly.
- wildcards
- Type: System.String[]
Pattern to be matched.
Return Value
Type: System.Collections.ObjectModel.ReadOnlyCollection<String>Read-only collection of file names from the specified directory.
| Exception | Condition |
|---|---|
| ArgumentException | 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 \\.\). |
| ArgumentNullException | directory is Nothing. |
| DirectoryNotFoundException | The directory to search does not exist. |
| IOException | directory points to an existing file. |
| PathTooLongException | The path exceeds the system-defined maximum length. |
| NotSupportedException | A file or directory name in the path contains a colon (:) or is in an invalid format. |
| SecurityException | The user lacks necessary permissions to view the path. |
| UnauthorizedAccessException | The user lacks necessary permissions. |
An empty collection is returned if no files matching the specified pattern are found.
The following table lists examples of tasks involving the My.Computer.FileSystem.GetFiles method.
To | See |
Get the collection of files in a directory | How to: Get the Collection of Files in a Directory in Visual Basic |
Find files with a specific pattern in a directory |
The following example returns all files in the directory and adds them to ListBox1.
For Each foundFile In My.Computer.FileSystem.GetFiles( My.Computer.FileSystem.SpecialDirectories.MyDocuments) ListBox1.Items.Add(foundFile) Next
This example requires that you have a ListBox named ListBox1 on your form.
This example returns all files in the directory with the extension .txt and adds them to ListBox1.
For Each foundFile As String In My.Computer.FileSystem.GetFiles( My.Computer.FileSystem.SpecialDirectories.MyDocuments, FileIO.SearchOption.SearchTopLevelOnly, "*.txt") ListBox1.Items.Add(foundFile) Next
This example requires that you have a ListBox named ListBox1 on your form.
- FileIOPermission
Controls the ability to access files and folders. Associated enumeration: Unrestricted.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.