C# – Get all files in a folder

There are two simple ways to get all files in a folder:

  • Use Directory.GetFiles() to get a string array containing all of the file paths.
  • Use Directory.EnumerateFiles() to efficiently loop over the file paths one at a time.

I’ll show examples below, along with a few other scenarios, such as getting files from subfolders.

Note: ‘directory’ and ‘folder’ mean the same thing. I use these terms interchangeably.

Get all files with Directory.GetFiles()

Directory.GetFiles() returns all file paths from the top-level folder in a string array. Here’s an example of using this:

using System.IO;

string[] filePaths = Directory.GetFiles(@"C:\temp\records\");

Console.WriteLine($"File count: {filePaths.Length}");

foreach(var filePath in filePaths)
{
    Console.WriteLine(filePath);
}
Code language: C# (cs)

Here’s what this outputs:

File count: 2
C:\temp\records\coding_hello_world.txt
C:\temp\records\hello_world.txtCode language: plaintext (plaintext)

Loop over all files with Directory.EnumerateFiles()

Directory.EnumerateFiles() returns an IEnumerable<string> containing the top-level folder’s file paths. This lets you efficiently loop over the files one at a time, instead of loading all file paths at once. Here’s an example of using this:

using System.IO;

var folderPath = @"C:\temp\records\";

foreach (var filePath in Directory.EnumerateFiles(folderPath))
{
    Console.WriteLine(filePath);
}
Code language: C# (cs)

This outputs the following:

C:\temp\records\coding_hello_world.txt
C:\temp\records\hello_world.txtCode language: plaintext (plaintext)

Get all files from a folder and subfolders

By default, the two methods shown above return files from the top-level folder. If you want to also get files from subfolders, pass in SearchOption.AllDirectories. Here’s an example:

using System.IO;

var folderPath = @"C:\temp\records\";

foreach (var filePath in Directory.EnumerateFiles(folderPath, "*", SearchOption.AllDirectories))
{
    Console.WriteLine(filePath);
}
Code language: C# (cs)

Note: When passing in SearchOption.AllDirectories, you also have to pass in the searchPattern parameter. Pass in “*” to match all files.

This returns files from the folder and its subfolders in breadth-first order (top-level first, then subfolders). This example outputs the following (notice the last two lines are files in subfolders):

C:\temp\records\coding_hello_world.txt
C:\temp\records\hello_world.txt
C:\temp\records\movies\Happy_Gilmore.txt
C:\temp\records\movies\Jurassic_Park.txt
Code language: plaintext (plaintext)

Get just the file names

The methods shown return the full file paths. If you just want the file names, use Path.GetFileName(). An efficient way to do this is to use Directory.EnumerateFiles() with Linq method Select() and Path.GetFileName(), like this:

using System.IO;
using System.Linq;

var folderPath = @"C:\temp\records\";

var fileNames = Directory.EnumerateFiles(folderPath).Select(filePath => Path.GetFileName(filePath));

foreach (var fileName in fileNames)
{
    Console.WriteLine(fileName);
}
Code language: C# (cs)

This outputs just the file names instead of the full paths:

coding_hello_world.txt
hello_world.txtCode language: plaintext (plaintext)

Get FileInfo objects instead of just file paths

Directory.GetFiles() / Directory.EnumerateFiles() return file path strings. Sometimes you may find it useful to get FileInfo objects instead, such as when you want additional info about the file, like when it was created. To do this, use DirectoryInfo.EnumerateFiles() instead. Here’s an example:

using System.IO;

var directoryInfo = new DirectoryInfo(@"C:\temp\records\");

foreach(FileInfo fileInfo in directoryInfo.EnumerateFiles())
{
    Console.WriteLine($"{fileInfo.CreationTime} {fileInfo.Name} ({fileInfo.Length} bytes)");
}
Code language: C# (cs)

This outputs the following:

8/4/2023 3:56:27 PM coding_hello_world.txt (11 bytes)
8/4/2023 3:56:27 PM hello_world.txt (275 bytes)Code language: plaintext (plaintext)