C# – Search for files in a directory

Use Directory.EnumerateFiles() to search for files in a directory and then loop over the file paths (and then reading the files, or whatever you want to do with the info). Here’s an example of searching for files containing “hello” in the file name:

using System.IO;

string directory = @"C:\temp\records\";
foreach (var file in Directory.EnumerateFiles(directory, searchPattern: "*hello*"))
{
    Console.WriteLine(file);
}
Code language: C# (cs)

Note: Use the wildcard character * to match anything.

It returns an IEnumerable<string> containing the full file paths. Here’s what this example outputs:

C:\temp\records\coding_hello_world.txt
C:\temp\records\hello.jsonCode language: plaintext (plaintext)

Use Directory.GetFiles() if you want file paths in a string[] instead (which can be useful in some scenarios). It has the same parameters as Directory.EnumerateFiles().

I’ll show more examples of searching for files in different scenarios.

Search for files in subdirectories

By default, Directory.EnumerateFiles() searches the top-level directory only. To search for files in all subdirectories, pass in SearchOption.AllDirectories, like this:

using System.IO;

string directory = @"C:\temp\records\";
foreach (var file in Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories))
{
    Console.WriteLine(file);
}
Code language: C# (cs)

Note: “*” means include ALL files in the search.

This outputs the following file paths. Notice it includes files from subdirectories. Note: It returns results in breadth-first order.

C:\temp\records\coding_hello_world.txt
C:\temp\records\hello.json
C:\temp\records\movies\Happy_Gilmore.txt
C:\temp\records\movies\Jurassic_Park.txtCode language: plaintext (plaintext)

Search for files by extension

To search for files by extension, use searchPattern starting with the wildcard character (*) and then the extension name. For example, here’s how to search for XML files (which you can then parse with XElement):

using System.IO;

string directory = @"C:\temp\records\";
foreach (var filePath in Directory.EnumerateFiles(directory, searchPattern: "*.xml"))
{
    Console.WriteLine(filePath);
}
Code language: C# (cs)

This outputs the XML file path (in the top-level directory only):

C:\temp\records\coding_hello_world.xmlCode language: plaintext (plaintext)

Search for the most recent file in a directory

Let’s say you want to find the most recently created file in a directory. To do that, use DirectoryInfo.EnumerateFiles() (instead of Directory.EnumerateFiles). This returns FileInfo objects, which you can then sort by creation time (CreationTimeUtc) and take the newest one. Here’s an example:

using System.IO;
using System.Linq;

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

var newestFile = directoryInfo.EnumerateFiles()
    .OrderByDescending(fileInfo => fileInfo.CreationTimeUtc)
    .First();

Console.WriteLine(newestFile.FullName);
Code language: C# (cs)

This outputs the full path of the newest file:

C:\temp\records\new_log.txtCode language: plaintext (plaintext)

Leave a Comment