Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Directory.GetFiles Method
- Reference
Definition
Returns the names of files that meet specified criteria.
Overloads
Returns the names of files [including their paths] in the specified directory.
Returns the names of files [including their paths] that match the specified search pattern in the specified directory.
Returns the names of files [including their paths] that match the specified search pattern and enumeration options in the specified directory.
Returns the names of files [including their paths] that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.
GetFiles[String]
Returns the names of files [including their paths] in the specified directory.
public:
static cli::array ^ GetFiles[System::String ^ path];
public static string[] GetFiles [string path];
static member GetFiles : string -> string[]
Public Shared Function GetFiles [path As String] As String[]
Parameters
path String
The relative or absolute path to the directory to search. This string is not case-sensitive.
Returns
An array of the full names [including paths] for the files in the specified directory, or an empty array if no files are found.
Exceptions
static member GetFiles : string -> string[]
4 is a file name.
-or-
A network error has occurred.
The caller does not have the required permission.
.NET Framework and .NET Core versions older than 2.1:
static member GetFiles : string -> string[]
4 is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the method.
The specified path, file name, or both exceed the system-defined maximum length.
The specified path is not found or is invalid [for example, it is on an unmapped drive].
Examples
The following example demonstrates how to use the GetFiles method to return file names from a user-specified location. The example is configured to catch all errors common to this method.
// For Directory::GetFiles and Directory::GetDirectories
// For File::Exists, Directory::Exists
using namespace System;
using namespace System::IO;
using namespace System::Collections;
// Insert logic for processing found files here.
void ProcessFile[ String^ path ]
{
Console::WriteLine[ "Processed file '{0}'.", path ];
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
void ProcessDirectory[ String^ targetDirectory ]
{
// Process the list of files found in the directory.
arrayfileEntries = Directory::GetFiles[ targetDirectory ];
IEnumerator files = fileEntries->GetEnumerator[];
while [ files->MoveNext[] ]
{
}
// Recurse into subdirectories of this directory.
arraysubdirectoryEntries = Directory::GetDirectories[ targetDirectory ];
IEnumerator dirs = subdirectoryEntries->GetEnumerator[];
while [ dirs->MoveNext[] ]
{ String^ fileName = safe_cast[files->Current];
ProcessFile[ fileName ];
}
}
int main[ int argc, char *argv[] ]
{
for [ int i = 1; i < argc; i++ ]
{ String^ subdirectory = safe_cast[dirs->Current];
ProcessDirectory[ subdirectory ];
}
}
String^ path = gcnew String[argv[ i ]];
if [ File::Exists[ path ] ]
{
// This path is a file
ProcessFile[ path ];
}
else
if [ Directory::Exists[ path ] ]
{
// This path is a directory
ProcessDirectory[ path ];
}
else
{
Console::WriteLine[ "{0} is not a valid file or directory.", path ];
}
// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;
public class RecursiveFileProcessor
{
}
public static void Main[string[] args]
{
foreach[string path in args]
{
if[File.Exists[path]]
{
// This path is a file
ProcessFile[path];
}
else if[Directory.Exists[path]]
{
// This path is a directory
ProcessDirectory[path];
}
else
{
Console.WriteLine["{0} is not a valid file or directory.", path];
}
}
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public static void ProcessDirectory[string targetDirectory]
{
// Process the list of files found in the directory.
string [] fileEntries = Directory.GetFiles[targetDirectory];
foreach[string fileName in fileEntries]
ProcessFile[fileName];
// Recurse into subdirectories of this directory.
string [] subdirectoryEntries = Directory.GetDirectories[targetDirectory];
foreach[string subdirectory in subdirectoryEntries]
ProcessDirectory[subdirectory];
}
// Insert logic for processing found files here.
public static void ProcessFile[string path]
{
Console.WriteLine["Processed file '{0}'.", path];
}
module RecursiveFileProcessor
open System.IO
// Insert logic for processing found files here.
let processFile path =
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
let rec processDirectory targetDirectory =printfn $"Processed file '%s{path}'."
[]
let main args =// Process the list of files found in the directory.
let fileEntries = Directory.GetFiles targetDirectory
for fileName in fileEntries do
processFile fileName
// Recurse into subdirectories of this directory.
let subdirectoryEntries = Directory.GetDirectories targetDirectory
for subdirectory in subdirectoryEntries do
processDirectory subdirectory
for path in args do
if File.Exists path then
// This path is a file
processFile path
elif Directory.Exists path then
// This path is a directory
processDirectory path
else
printfn $"{path} is not a valid file or directory."
0
' For Directory.GetFiles and Directory.GetDirectories
' For File.Exists, Directory.Exists
Imports System.IO
Imports System.Collections
Public Class RecursiveFileProcessor
End Class
Public Overloads Shared Sub Main[ByVal args[] As String]
Dim path As String
For Each path In args
If File.Exists[path] Then
' This path is a file.
ProcessFile[path]
Else
If Directory.Exists[path] Then
' This path is a directory.
ProcessDirectory[path]
Else
Console.WriteLine["{0} is not a valid file or directory.", path]
End If
End If
Next path
End Sub
' Process all files in the directory passed in, recurse on any directories
' that are found, and process the files they contain.
Public Shared Sub ProcessDirectory[ByVal targetDirectory As String]
Dim fileEntries As String[] = Directory.GetFiles[targetDirectory]
' Process the list of files found in the directory.
Dim fileName As String
For Each fileName In fileEntries
ProcessFile[fileName]
Next fileName
Dim subdirectoryEntries As String[] = Directory.GetDirectories[targetDirectory]
' Recurse into subdirectories of this directory.
Dim subdirectory As String
For Each subdirectory In subdirectoryEntries
ProcessDirectory[subdirectory]
Next subdirectory
End Sub
' Insert logic for processing found files here.
Public Shared Sub ProcessFile[ByVal path As String]
Console.WriteLine["Processed file '{0}'.", path]
End Sub
Remarks
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 returned file names are appended to the supplied
static member GetFiles : string -> string[]
4 parameter.
This method is identical to with the asterisk [*] specified as the search pattern.
The
static member GetFiles : string -> string[]
4 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 order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.
The case-sensitivity of the
static member GetFiles : string -> string[]
4 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.
See also
- File and Stream I/O
- How to: Read Text from a File
- How to: Write Text to a File
Applies to
GetFiles[String, String]
Returns the names of files [including their paths] that match the specified search pattern in the specified directory.
public:
static cli::array ^ GetFiles[System::String ^ path, System::String ^ searchPattern];
public static string[] GetFiles [string path, string searchPattern];
public static string[] GetFiles [string path];
0
public static string[] GetFiles [string path];
1
Parameters
path String
The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern String
The search string to match against the names of files in
static member GetFiles : string -> string[]
4. This parameter can contain a combination of valid literal path and wildcard [* and ?] characters, but it doesn't support regular expressions.
Returns
An array of the full names [including paths] for the files in the specified directory that match the specified search pattern, or an empty array if no files are found.
Exceptions
static member GetFiles : string -> string[]
4 is a file name.
-or-
A network error has occurred.
The caller does not have the required permission.
.NET Framework and .NET Core versions older than 2.1:
static member GetFiles : string -> string[]
4 is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using .
-or-
Public Shared Function GetFiles [path As String] As String[]
2 doesn't contain a valid pattern.
static member GetFiles : string -> string[]
4 or
Public Shared Function GetFiles [path As String] As String[]
2 is
Public Shared Function GetFiles [path As String] As String[]
5.
The specified path, file name, or both exceed the system-defined maximum length.
The specified path is not found or is invalid [for example, it is on an unmapped drive].
Examples
The following example counts the number of files that begin with the specified letter.
public static string[] GetFiles [string path];
2
public static string[] GetFiles [string path];
3
public static string[] GetFiles [string path];
4
public static string[] GetFiles [string path];
5
Remarks
The returned file names are appended to the supplied
static member GetFiles : string -> string[]
4 parameter and the order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.
Public Shared Function GetFiles [path As String] As String[]
2 can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in
Public Shared Function GetFiles [path As String] As String[]
2.
Wildcard specifier Matches * [asterisk] Zero or more characters in that position. ? [question mark] Exactly one character in that position.
Characters other than the wildcard are literal characters. For example, the
Public Shared Function GetFiles [path As String] As String[]
2 string "*t" searches for all names in
static member GetFiles : string -> string[]
4 ending with the letter "t". The
Public Shared Function GetFiles [path As String] As String[]
2 string "s*" searches for all names in
static member GetFiles : string -> string[]
4 beginning with the letter "s".
Public Shared Function GetFiles [path As String] As String[]
2 cannot end in two periods [".."] or contain two periods [".."] followed by or , nor can it contain any invalid characters. You can query for invalid characters by using the GetInvalidPathChars method.
Note
.NET Framework only: When you use the asterisk wildcard character in
Public Shared Function GetFiles [path As String] As String[]
2 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 file.ai, file.aif *.ai file.ai file.ai book.xls, book.xlsx *.xls book.xls book.xls, book.xlsx ello.txt, hello.txt, hello.txtt ?ello.txt ello.txt, hello.txt ello.txt, hello.txt
Note
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
static member GetFiles : string -> string[]
4 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
static member GetFiles : string -> string[]
4 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.
See also
- File and Stream I/O
- How to: Read Text from a File
- How to: Write Text to a File
Applies to
GetFiles[String, String, EnumerationOptions]
Returns the names of files [including their paths] that match the specified search pattern and enumeration options in the specified directory.
public static string[] GetFiles [string path];
6
public static string[] GetFiles [string path];
7
public static string[] GetFiles [string path];
8
public static string[] GetFiles [string path];
9
Parameters
path String
The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern String
The search string to match against the names of subdirectories in
static member GetFiles : string -> string[]
4. This parameter can contain a combination of valid literal and wildcard characters, but it doesn't support regular expressions.
enumerationOptions EnumerationOptions
An object that describes the search and enumeration configuration to use.
Returns
An array of the full names [including paths] for the files in the specified directory that match the specified search pattern and enumeration options, or an empty array if no files are found.
Exceptions
static member GetFiles : string -> string[]
4 is a file name.
-or-
A network error has occurred.
The caller does not have the required permission.
.NET Framework and .NET Core versions older than 2.1:
static member GetFiles : string -> string[]
4 is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using .
-or-
Public Shared Function GetFiles [path As String] As String[]
2 doesn't contain a valid pattern.
static member GetFiles : string -> string[]
4 or
Public Shared Function GetFiles [path As String] As String[]
2 is
Public Shared Function GetFiles [path As String] As String[]
5.
The specified path, file name, or both exceed the system-defined maximum length.
The specified path is not found or is invalid [for example, it is on an unmapped drive].
Remarks
The returned file names are appended to the supplied
static member GetFiles : string -> string[]
4 parameter and the order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.
Public Shared Function GetFiles [path As String] As String[]
2 can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in
Public Shared Function GetFiles [path As String] As String[]
2.
Wildcard specifier Matches * [asterisk] Zero or more characters in that position. ? [question mark] Exactly one character in that position.
Characters other than the wildcard are literal characters. For example, the
Public Shared Function GetFiles [path As String] As String[]
2 string "*t" searches for all names in
static member GetFiles : string -> string[]
4 ending with the letter "t". The
Public Shared Function GetFiles [path As String] As String[]
2 string "s*" searches for all names in
static member GetFiles : string -> string[]
4 beginning with the letter "s".
Public Shared Function GetFiles [path As String] As String[]
2 cannot end in two periods [".."] or contain two periods [".."] followed by or , nor can it contain any invalid characters. You can query for invalid characters by using the GetInvalidPathChars method.
Note
.NET Framework only: When you use the asterisk wildcard character in
Public Shared Function GetFiles [path As String] As String[]
2 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 file.ai, file.aif *.ai file.ai file.ai book.xls, book.xlsx *.xls book.xls book.xls, book.xlsx ello.txt, hello.txt, hello.txtt ?ello.txt ello.txt, hello.txt ello.txt, hello.txt
Note
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
static member GetFiles : string -> string[]
4 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
static member GetFiles : string -> string[]
4 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.
Applies to
GetFiles[String, String, SearchOption]
Returns the names of files [including their paths] that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.
static member GetFiles : string -> string[]
0
static member GetFiles : string -> string[]
1
static member GetFiles : string -> string[]
2
static member GetFiles : string -> string[]
3
Parameters
path String
The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern String
The search string to match against the names of files in
static member GetFiles : string -> string[]
4. This parameter can contain a combination of valid literal path and wildcard [* and ?] characters, but it doesn't support regular expressions.
searchOption SearchOption
One of the enumeration values that specifies whether the search operation should include all subdirectories or only the current directory.
Returns
An array of the full names [including paths] for the files in the specified directory that match the specified search pattern and option, or an empty array if no files are found.
Exceptions
.NET Framework and .NET Core versions older than 2.1:
static member GetFiles : string -> string[]
4 is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters with the method.
-or-
Public Shared Function GetFiles [path As String] As String[]
2 does not contain a valid pattern.
static member GetFiles : string -> string[]
4 or
module RecursiveFileProcessor
open System.IO
// Insert logic for processing found files here.
let processFile path =
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
let rec processDirectory targetDirectory =printfn $"Processed file '%s{path}'."
[]
let main args =// Process the list of files found in the directory.
let fileEntries = Directory.GetFiles targetDirectory
for fileName in fileEntries do
processFile fileName
// Recurse into subdirectories of this directory.
let subdirectoryEntries = Directory.GetDirectories targetDirectory
for subdirectory in subdirectoryEntries do
processDirectory subdirectory
for path in args do
if File.Exists path then
// This path is a file
processFile path
elif Directory.Exists path then
// This path is a directory
processDirectory path
else
printfn $"{path} is not a valid file or directory."
0
9 is
Public Shared Function GetFiles [path As String] As String[]
5.
The caller does not have the required permission.
The specified path is not found or is invalid [for example, it is on an unmapped drive].
The specified path, file name, or both exceed the system-defined maximum length.
static member GetFiles : string -> string[]
4 is a file name.
-or-
A network error has occurred.
Remarks
The returned file names are appended to the supplied parameter
static member GetFiles : string -> string[]
4 and the order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.
Public Shared Function GetFiles [path As String] As String[]
2 can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in
Public Shared Function GetFiles [path As String] As String[]
2.
Wildcard specifier Matches * [asterisk] Zero or more characters in that position. ? [question mark] Exactly one character in that position.
Characters other than the wildcard are literal characters. For example, the
Public Shared Function GetFiles [path As String] As String[]
2 string "*t" searches for all names in
static member GetFiles : string -> string[]
4 ending with the letter "t". The
Public Shared Function GetFiles [path As String] As String[]
2 string "s*" searches for all names in
static member GetFiles : string -> string[]
4 beginning with the letter "s".
Public Shared Function GetFiles [path As String] As String[]
2 cannot end in two periods [".."] or contain two periods [".."] followed by or , nor can it contain any invalid characters. You can query for invalid characters by using the GetInvalidPathChars method.
Note
.NET Framework only: When you use the asterisk wildcard character in
Public Shared Function GetFiles [path As String] As String[]
2 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 file.ai, file.aif *.ai file.ai file.ai book.xls, book.xlsx *.xls book.xls book.xls, book.xlsx ello.txt, hello.txt, hello.txtt ?ello.txt ello.txt, hello.txt ello.txt, hello.txt
Note
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
static member GetFiles : string -> string[]
4 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
static member GetFiles : string -> string[]
4 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.
See also
- File and Stream I/O
- How to: Read Text from a File
- How to: Write Text to a File
Applies to
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.