Imports System.IO
Public Class Test
Public Shared Sub Main()
' Only get files that begin with the letter "c".
Dim dirs As String() = Directory.GetFiles("c:\", "c*")
Console.WriteLine("The number of files starting with c is {0}.", dirs.Length)
Dim dir As String
For Each dir In dirs
Console.WriteLine(dir)
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
Remarks
The returned file names are appended to the supplied
path
parameter and the order of the returned file names is not guaranteed; use the
Sort
method if a specific sort order is required.
searchPattern
can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in
searchPattern
.
Wildcard specifier
Matches
Characters other than the wildcard are literal characters. For example, the
searchPattern
string "*t" searches for all names in
path
ending with the letter "t". The
searchPattern
string "s*" searches for all names in
path
beginning with the letter "s".
searchPattern
cannot end in two periods ("..") or contain two periods ("..") followed by
DirectorySeparatorChar
or
AltDirectorySeparatorChar
, nor can it contain any invalid characters. You can query for invalid characters by using the
GetInvalidPathChars
method.
.NET Framework only:
When you use the asterisk wildcard character in
searchPattern
and you specify a three-character file extension, for example, "*.txt", this method also returns files with extensions that
begin
with the specified extension. For example, the search pattern "*.xls" returns both "book.xls" and "book.xlsx". This behavior only occurs if an asterisk is used in the search pattern and the file extension provided is exactly three characters. If you use the question mark wildcard character somewhere in the search pattern, this method returns only files that match the specified file extension exactly. The following table depicts this anomaly in .NET Framework.
Files in directory
Search pattern
.NET 5+ returns
.NET Framework returns
Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".
The
EnumerateFiles
and
GetFiles
methods differ as follows: When you use
EnumerateFiles
, you can start enumerating the collection of names before the whole collection is returned; when you use
GetFiles
, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories,
EnumerateFiles
can be more efficient.
The
path
parameter can specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see
GetCurrentDirectory
.
The case-sensitivity of the
path
parameter corresponds to that of the file system on which the code is running. For example, it's case-insensitive on NTFS (the default Windows file system) and case-sensitive on Linux file systems.
For a list of common I/O tasks, see
Common I/O Tasks
.
public:
static cli::array <System::String ^> ^ GetFiles(System::String ^ path, System::String ^ searchPattern, System::IO::EnumerationOptions ^ enumerationOptions);
public static string[] GetFiles (string path, string searchPattern, System.IO.EnumerationOptions enumerationOptions);
static member GetFiles : string * string * System.IO.EnumerationOptions -> string[]
Public Shared Function GetFiles (path As String, searchPattern As String, enumerationOptions As EnumerationOptions) As String()
Parameters
ArgumentException
.NET Framework and .NET Core versions older than 2.1:
path
is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using
GetInvalidPathChars()
.
searchPattern
doesn't contain a valid pattern.
Remarks
The returned file names are appended to the supplied
path
parameter and the order of the returned file names is not guaranteed; use the
Sort
method if a specific sort order is required.
searchPattern
can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in
searchPattern
.
Wildcard specifier
Matches
Characters other than the wildcard are literal characters. For example, the
searchPattern
string "*t" searches for all names in
path
ending with the letter "t". The
searchPattern
string "s*" searches for all names in
path
beginning with the letter "s".
searchPattern
cannot end in two periods ("..") or contain two periods ("..") followed by
DirectorySeparatorChar
or
AltDirectorySeparatorChar
, nor can it contain any invalid characters. You can query for invalid characters by using the
GetInvalidPathChars
method.
.NET Framework only:
When you use the asterisk wildcard character in
searchPattern
and you specify a three-character file extension, for example, "*.txt", this method also returns files with extensions that
begin
with the specified extension. For example, the search pattern "*.xls" returns both "book.xls" and "book.xlsx". This behavior only occurs if an asterisk is used in the search pattern and the file extension provided is exactly three characters. If you use the question mark wildcard character somewhere in the search pattern, this method returns only files that match the specified file extension exactly. The following table depicts this anomaly in .NET Framework.
Files in directory
Search pattern
.NET 5+ returns
.NET Framework returns
Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".
The
EnumerateFiles
and
GetFiles
methods differ as follows: When you use
EnumerateFiles
, you can start enumerating the collection of names before the whole collection is returned; when you use
GetFiles
, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories,
EnumerateFiles
can be more efficient.
The
path
parameter can specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see
GetCurrentDirectory
.
The case-sensitivity of the
path
parameter corresponds to that of the file system on which the code is running. For example, it's case-insensitive on NTFS (the default Windows file system) and case-sensitive on Linux file systems.
For a list of common I/O tasks, see
Common I/O Tasks
.
public:
static cli::array <System::String ^> ^ GetFiles(System::String ^ path, System::String ^ searchPattern, System::IO::SearchOption searchOption);
public static string[] GetFiles (string path, string searchPattern, System.IO.SearchOption searchOption);
static member GetFiles : string * string * System.IO.SearchOption -> string[]
Public Shared Function GetFiles (path As String, searchPattern As String, searchOption As SearchOption) As String()
Parameters
One of the enumeration values that specifies whether the search operation should include all subdirectories or only the current directory.
Returns
ArgumentException
.NET Framework and .NET Core versions older than 2.1:
path
is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters with the
GetInvalidPathChars()
method.
searchPattern
does not contain a valid pattern.
Remarks
The returned file names are appended to the supplied parameter
path
and the order of the returned file names is not guaranteed; use the
Sort
method if a specific sort order is required.
searchPattern
can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in
searchPattern
.
Wildcard specifier
Matches
Characters other than the wildcard are literal characters. For example, the
searchPattern
string "*t" searches for all names in
path
ending with the letter "t". The
searchPattern
string "s*" searches for all names in
path
beginning with the letter "s".
searchPattern
cannot end in two periods ("..") or contain two periods ("..") followed by
DirectorySeparatorChar
or
AltDirectorySeparatorChar
, nor can it contain any invalid characters. You can query for invalid characters by using the
GetInvalidPathChars
method.
.NET Framework only:
When you use the asterisk wildcard character in
searchPattern
and you specify a three-character file extension, for example, "*.txt", this method also returns files with extensions that
begin
with the specified extension. For example, the search pattern "*.xls" returns both "book.xls" and "book.xlsx". This behavior only occurs if an asterisk is used in the search pattern and the file extension provided is exactly three characters. If you use the question mark wildcard character somewhere in the search pattern, this method returns only files that match the specified file extension exactly. The following table depicts this anomaly in .NET Framework.
Files in directory
Search pattern
.NET 5+ returns
.NET Framework returns
Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".
The
EnumerateFiles
and
GetFiles
methods differ as follows: When you use
EnumerateFiles
, you can start enumerating the collection of names before the whole collection is returned; when you use
GetFiles
, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories,
EnumerateFiles
can be more efficient.
The file names include the full path.
The
path
parameter can specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see
GetCurrentDirectory
.
The case-sensitivity of the
path
parameter corresponds to that of the file system on which the code is running. For example, it's case-insensitive on NTFS (the default Windows file system) and case-sensitive on Linux file systems.
For a list of common I/O tasks, see
Common I/O Tasks
.