C# Recursive File List

Dot Net Perls
File with lines of text

You want to acquire a list of all the files in a certain folder, and also all the files in each subdirectory, from the operating system, using the C# programming language. Although complex methods exist that use recursion or other implementations, the .NET Framework provides the SearchOption.AllDirectories enum argument to the Directory.GetFiles method—this offloads the complexity to the framework designers.

Example

In this first example, we propose a program that gets a string array of all the files at a certain level of the file system, and also all sublevels. Then, it loops through the result and prints the file paths. You can see that the first level files in the specified directory are printed, and then all subdirectory files as well.

String Array Foreach Loop Examples Console.WriteLine Use
Program that lists files recursively [C#]

using System;
using System.IO;

class Program
{
    static void Main()
    {
	// Get list of files in the specific directory.
	// ... Please change the first argument.
	string[] files = Directory.GetFiles("C:\\PerlsComStage\\",
	    "*.*",
	    SearchOption.AllDirectories);

	// Display all the files.
	foreach (string file in files)
	{
	    Console.WriteLine(file);
	}
    }
}

Output

C:\PerlsComStage\Default.aspx
C:\PerlsComStage\Global.asax
C:\PerlsComStage\Web.config
C:\PerlsComStage\bin\PerlsComWebProject1.dll
C:\PerlsComStage\bin\PerlsComWebProject1.pdb

Description. The first argument to the Directory.GetFiles method is the starting path—you must escape the backslashes in Windows paths somehow. We use the double-backslash sequence to do this. The second argument to the invocation is the universal pattern for file names; if you change the asterisks to a string, you can actually filter your files. The third argument is the enumerated constant SearchOption.AllDirectories, which indicates you want a recursive file search.

Enum Examples

List example

List type.

The List generic type is more useful for many programs. In this example, we convert the array returned by Directory.GetFiles to a List instance. Then we pass the List instance to another method as a formal parameter and display the value returned by its Count property.

List Examples
Program that gets file List [C#]

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main()
    {
	// Make sure directory exists before using this!
	var files = new List<string>(Directory.GetFiles("C:\\folder",
	    "*.*",
	    SearchOption.AllDirectories));
	Method(files);
    }

    static void Method(List<string> files)
    {
	Console.WriteLine(files.Count);
    }
}

Output
   (Varies depending on contents of the folder.)

22

Note: In early versions of the .NET Framework, it may have been necessary to implement custom recursive file search algorithms. Today these methods are unnecessary because they overlap with existing functionality.

Summary

The C# programming language

This overloaded version of the Directory.GetFiles method allows you to perform a recursive file listing in your C# program. Instead of implementing elaborate custom implementations, this method overload provides a clearer, simpler abstraction. While implementing custom recursion and stack-based methods is interesting, using a proven method developed at Microsoft is more effective in typical programming contexts.

Path Examples