C# Recursive File List

Page: lines of text

Directories are often nested. Sometimes we need a list of files in a folder and those in each subdirectory. The .NET Framework allows a SearchOption.AllDirectories argument to the Directory.GetFiles and EnumerateFiles methods.

Based on:

.NET 4.5

GetFiles

In this first example, we show a program that gets a string array of all the files at a certain level of the file system. It also covers all sublevels. Then, it loops through the result and prints the file paths.

One: 1

Tip:You can see that the first level files in the specified directory are printed, and then all subdirectory files as well.

String ArraysForeachConsole.WriteLine
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
Star, asterisk character

The first argument to Directory.GetFiles is the starting path. You must escape the backslashes in Windows paths. We use the double-backslash sequence to do this. The second argument is the universal pattern for file names.

Tip:If you change the asterisks to a string, you can actually filter your files.

And:The third argument is the enumerated constant SearchOption.AllDirectories, which indicates you want a recursive file search.

Enumerated Constant

List

List type

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

Note:In early versions of the .NET Framework, it may have been necessary to implement custom recursive file search algorithms.

But:Today these methods are unnecessary because they overlap with existing functionality.

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

EnumerateFiles

Three

Another option for getting files in nested directories is EnumerateFiles. This method receives the directory path as its first argument. The second two arguments are optional. To recurse through directories, use AllDirectories.

Tip:With EnumerateFiles, we receive an IEnumerable<string>. This must be evaluated in a foreach-loop or extension method.

IEnumerable
Program that uses EnumerateFiles: C#

using System;
using System.IO;

class Program
{
    static void Main()
    {
	// Call EnumerateFiles in a foreach-loop.
	foreach (string file in Directory.EnumerateFiles(@"c:\files",
	    "*.*",
	    SearchOption.AllDirectories))
	{
	    // Display file path.
	    Console.WriteLine(file);
	}
    }
}

Output

c:\files\index.html
c:\files\style.css
c:\files\images\logo.png
c:\files\images\picture.jpg

Note:Thanks to Csaba Toth for pointing out the EnumerateFiles method, added in the .NET Framework 4.0.

Summary

C# programming language

An overloaded version of the Directory.GetFiles method allows you to perform a recursive file listing. Instead of elaborate custom implementations, this method overload provides a clearer, simpler abstraction.

And:The EnumerateFiles is another option. Its usage is similar to GetFiles, but it returns an IEnumerable.

Thus:Implementing custom recursion and stack-based methods is interesting. But using a proven method is often more effective.


C#: File: Path