Directory.GetFiles returns the full paths of the files in a folder as a string[]. Directory.EnumerateFiles returns the same paths as a lazy IEnumerable<string>. Both are in System.IO.
The examples assume this folder structure:
C:\TestDirectory\Files\file1.png
C:\TestDirectory\Files\file2.avi
C:\TestDirectory\Files\file3.jpg
C:\TestDirectory\Files\SubFolder\file4.jpg
C:\TestDirectory\Files\SubFolder\file5.avi
All files in one folder
string[] files = Directory.GetFiles(@"C:\TestDirectory\Files");
foreach (string file in files)
{
Console.WriteLine(file);
}
// C:\TestDirectory\Files\file1.png
// C:\TestDirectory\Files\file2.avi
// C:\TestDirectory\Files\file3.jpg
Only the top folder is searched; SubFolder and its files are not included. The results are full paths. Use Path.GetFileName(file) if you only want the name.
The order is whatever the file system returns. NTFS usually returns names alphabetically, Linux file systems usually do not, and the documentation guarantees nothing. If you display the list or depend on its order, sort it: Array.Sort(files, StringComparer.OrdinalIgnoreCase).
Filter by extension
The second parameter is a search pattern. * matches any number of characters and ? matches exactly one:
string[] videos = Directory.GetFiles(@"C:\TestDirectory\Files", "*.avi");
// C:\TestDirectory\Files\file2.avi
There is no syntax for several patterns at once, such as *.jpg;*.png. Enumerate everything and filter with LINQ instead:
var imageExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".jpg", ".png" };
var images = Directory
.EnumerateFiles(@"C:\TestDirectory\Files")
.Where(file => imageExtensions.Contains(Path.GetExtension(file)));
// C:\TestDirectory\Files\file1.png
// C:\TestDirectory\Files\file3.jpg
Compare extensions with an ordinal, case-insensitive comparer. A plain file.EndsWith(".jpg") misses PHOTO.JPG and, without StringComparison.Ordinal, also runs a culture-sensitive comparison you do not need.
On .NET Framework, a pattern with a three-letter extension such as *.htm also matches .html files, because Windows compares it against the old 8.3 short names too. If that bites you, filter the results again with Path.GetExtension.
Include subfolders
Pass SearchOption.AllDirectories as the third argument:
string[] videos = Directory.GetFiles(
@"C:\TestDirectory\Files", "*.avi", SearchOption.AllDirectories);
// C:\TestDirectory\Files\file2.avi
// C:\TestDirectory\Files\SubFolder\file5.avi
This version has a well-known flaw: if the search reaches a single folder you are not allowed to read, it throws UnauthorizedAccessException and you get no results at all. Searching from C:\ or a user profile almost always hits one.
EnumerationOptions for real-world searches
Since .NET Core 2.1 there is an overload that takes EnumerationOptions, which fixes that and a few other problems:
var options = new EnumerationOptions
{
RecurseSubdirectories = true,
IgnoreInaccessible = true, // skip folders you cannot read
MatchCasing = MatchCasing.CaseInsensitive // same results on Windows and Linux
};
foreach (string file in Directory.EnumerateFiles(@"C:\TestDirectory\Files", "*.avi", options))
{
Console.WriteLine(file);
}
MatchCasing.CaseInsensitive matters if your code also runs on Linux, where the default is case-sensitive and *.avi would miss MOVIE.AVI.
One difference to know: a new EnumerationOptions skips hidden and system files by default (AttributesToSkip is Hidden | System), while the overloads without options include them. Set AttributesToSkip = 0 if you want everything.
GetFiles or EnumerateFiles
GetFiles walks the whole tree and builds the full array before it returns. EnumerateFiles hands you each path as soon as it is found, so you can start working right away, stop early with First or Take, and avoid holding a huge array in memory. For a large tree, EnumerateFiles is the better default. The flip side: because it is lazy, the folder is read while you iterate, and an exception can surface inside your foreach instead of at the call.
When you need size or dates as well as names, use DirectoryInfo.EnumerateFiles. It returns FileInfo objects filled from the same directory read, so properties such as Length and LastWriteTime do not cost an extra call per file:
var largeFiles = new DirectoryInfo(@"C:\TestDirectory\Files")
.EnumerateFiles("*", SearchOption.AllDirectories)
.Where(f => f.Length > 10 * 1024 * 1024)
.OrderByDescending(f => f.Length);
foreach (FileInfo f in largeFiles)
{
Console.WriteLine($"{f.FullName} {f.Length / 1024 / 1024} MB");
}