FileSystem.GetFiles FileSystem.GetFiles FileSystem.GetFiles FileSystem.GetFiles Method

Definition

Returns a read-only collection of strings representing the names of files within a directory.

Overloads

GetFiles(String) GetFiles(String) GetFiles(String) GetFiles(String)

Returns a read-only collection of strings representing the names of files within a directory.

GetFiles(String, SearchOption, String[]) GetFiles(String, SearchOption, String[]) GetFiles(String, SearchOption, String[]) GetFiles(String, SearchOption, String[])

Returns a read-only collection of strings representing the names of files within a directory.

GetFiles(String) GetFiles(String) GetFiles(String) GetFiles(String)

Returns a read-only collection of strings representing the names of files within a directory.

public:
 static System::Collections::ObjectModel::ReadOnlyCollection<System::String ^> ^ GetFiles(System::String ^ directory);
public static System.Collections.ObjectModel.ReadOnlyCollection<string> GetFiles (string directory);
static member GetFiles : string -> System.Collections.ObjectModel.ReadOnlyCollection<string>
Public Shared Function GetFiles (directory As String) As ReadOnlyCollection(Of String)

Parameters

directory
String String String String

Directory to be searched.

Returns

Read-only collection of file names from the specified directory.

Exceptions

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 \\.\).

directory points to an existing file.

The path exceeds the system-defined maximum length.

A file or directory name in the path contains a colon (:) or is in an invalid format.

The user lacks necessary permissions to view the path.

Examples

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.

Remarks

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 How to: Find Files with a Specific Pattern in Visual Basic

Security

FileIOPermission
Controls the ability to access files and folders. Associated enumeration: Unrestricted.

See also

GetFiles(String, SearchOption, String[]) GetFiles(String, SearchOption, String[]) GetFiles(String, SearchOption, String[]) GetFiles(String, SearchOption, String[])

Returns a read-only collection of strings representing the names of files within a directory.

public:
 static System::Collections::ObjectModel::ReadOnlyCollection<System::String ^> ^ GetFiles(System::String ^ directory, Microsoft::VisualBasic::FileIO::SearchOption searchType, ... cli::array <System::String ^> ^ wildcards);
public static System.Collections.ObjectModel.ReadOnlyCollection<string> GetFiles (string directory, Microsoft.VisualBasic.FileIO.SearchOption searchType, params string[] wildcards);
static member GetFiles : string * Microsoft.VisualBasic.FileIO.SearchOption * string[] -> System.Collections.ObjectModel.ReadOnlyCollection<string>
Public Shared Function GetFiles (directory As String, searchType As SearchOption, ParamArray wildcards As String()) As ReadOnlyCollection(Of String)

Parameters

directory
String String String String

Directory to be searched.

searchType
SearchOption SearchOption SearchOption SearchOption

Whether to include subfolders. Default is SearchOption.SearchTopLevelOnly.

wildcards
String[]

Pattern to be matched.

Returns

Read-only collection of file names from the specified directory.

Exceptions

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 \\.\).

directory points to an existing file.

The path exceeds the system-defined maximum length.

A file or directory name in the path contains a colon (:) or is in an invalid format.

The user lacks necessary permissions to view the path.

Examples

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.

Remarks

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 How to: Find Files with a Specific Pattern in Visual Basic

Security

FileIOPermission
Controls the ability to access files and folders. Associated enumeration: Unrestricted.

See also

Applies to