Listing files from directories and sub-directories is a common requirement for many developers. In this short tutorial I will show you how to do this in two different ways.
The Directory.GetFiles() Method
This first method is by far the easier of the two, but to implement this you must be working with Microsoft .NET Framework version 2.0 or later.
The System.IO.Directory
class contains a method called GetFiles()
. It returns a string array of full file names for each file it finds. It also accepts a number of parameters which allow you to customize your search. Below are examples of the three overloads for GetFiles()
.
Here we are getting a string array of all the files within the directory “E:\Music\Dire Straits”.
string[] files = Directory.GetFiles("E:\\Music\\Dire Straits");
Now here we are filtering which files to get by file extension – we are getting only the files with an extension of “.mp3” from “E:\Music\Dire Straits.”
string[] files = Directory.GetFiles("E:\\Music\\Dire Straits", "*.mp3");
Finally here we are getting all the “.mp3” files from “E:\Music\Dire Straits” and all its sub-directories.
string[] files = Directory.GetFiles("E:\\Music\\Dire Straits", "*.mp3", SearchOption.AllDirectories);
As you can see, the guys from Microsoft have made getting files from directories recursively very easy – it is just one line of code!
Create a Recursive Search Method
If you are running Microsoft .NET Framework 1.1 you will have to create your own method to recursively search your directory since the GetFiles()
method does not support the SearchOption overload.
A recursive method is basically a method which calls itself. Recursion is a very useful technique but can also be demanding on memory if the recursion gets too deep.
So, let’s create a recursive method which will search for all “mp3” files within a given directory and its sub-directories.
private void DirSearchMP3(string directory) { foreach (string dir in Directory.GetDirectories(directory)) { foreach (string file in Directory.GetFiles(dir, "*.mp3")) { this.files.Add(file); } this.DirSearchMP3(dir); } }
As you can see in the above example, we are iterating all the directories within the given directory passed as a parameter, and then searching for “.mp3” files within each directory. Then the DirSearchMP3()
method calls itself and starts the whole process again. It keeps doing this until no more sub-directories are found.
Below is the whole file listing for this example.
using System; using System.Collections.Generic; using System.Windows.Forms; using System.IO; namespace RecursiveFileSearch { public partial class Form1 : Form { Listfiles = new List (); public Form1() { InitializeComponent(); } private void btnSearch_Click(object sender, EventArgs e) { // Search for mp3 files within all sub-directories directories this.DirSearchMP3("E:\\Music\\Dire Straits"); // Search for mp3 files within current directory foreach (string file in Directory.GetFiles("E:\\Music\\Dire Straits", "*.mp3")) { this.files.Add(file); } // Display all files found foreach (string file in this.files) { Console.WriteLine(file); } } private void DirSearchMP3(string directory) { foreach (string dir in Directory.GetDirectories(directory)) { foreach (string file in Directory.GetFiles(dir, "*.mp3")) { this.files.Add(file); } this.DirSearchMP3(dir); } } } }
As you can see from this small tutorial, recursively searching directories is much easier with the .NET 2.0 framework.
Dave
Dave I get troubled by people like you who say things like “The guys at Microsoft have made it pretty easy…”
Do you not take into account the gargantuan man-hourse of time they have cost us with their shear stupidity?
That’s fair enough Kevin, everyone is entitled to their opinion.