
Introduction
This class library is primarily a utility for a larger application that might need to collate a list of files from a directory. It provides a simple means of returning a collection of files present in a directory, with the option of having the search recurse down all the directories' sub-directories. I've also implemented a way to define a collection of file extensions that filters the collection returned by the search. I wanted this feature so I could define a search that only returned all the *.mp3 files found in a directory.
Basics of the RecursiveFileExplorer Namespace
The very first thing you need is a brief explanation of the RecursiveFileExplorer Namespace and what it contains. Two classes are defined in the namespace:
public class FileExplorer
public class FileData
FileExplorer is the class you would implement to search a directory for files. FileData is the container class used by FileExplorer to store details of the files it finds. To use the RecursiveFileExplorer Namespace, simply include the following at the top of your file.
using RecursiveFileExplorer;
Using the FileExplorer Class
Using the FileExplorer class is deliberately simple. You simply pass it the path to the directory you want to search and it creates a collection of all the files in the directory in the form of an ArrayList object. This ArrayList can then be bound to a data control, or iterated through in some other fashion. The code to do this is as follows:
FileExplorer FileExplorer = new FileExplorer( "c:/" );
this.ResultsBox.DataSource = FileExplorer.FileList;
Now if you want to get the added functionality of recursing through the sub-directories as well, you need to use one of the other overloaded constructors that takes a boolean value that indicates whether to use recursion. Like so:
FileExplorer FileExplorer = new FileExplorer( "c:/", true );
this.ResultsBox.DataSource = FileExplorer.FileList;
To apply a filter to the files collated into the collection, use another of the other overloaded constructors that takes an ArrayList of file extensions. Like so:
ArrayList Extensions = new ArrayList();
Extensions.Add(".mp3");
Extensions.Add(".wav");
FileExplorer FileExplorer = new FileExplorer( "c:/", Extensions );
this.ResultsBox.DataSource = FileExplorer.FileList;
And of course, you can use all three options if you really want to wallow in excess:
ArrayList Extensions = new ArrayList();
Extensions.Add(".mp3");
Extensions.Add(".wav");
FileExplorer FileExplorer = new FileExplorer( "c:/", Extensions, true );
this.ResultsBox.DataSource = FileExplorer.FileList;
So what's the FileData Class for again...
When the FileExplorer collates the files it finds into a collection it actually stores more than just the fully qualified file name, it also stores the simple filename, the file extension and the length of the file in bytes, and it stores this data in an instance of the FileData class which it then adds to the ArrayList collection. This functionality is provided if you would like to extend the functionality of the class library. For instance I have an extension of this class library that stores ID2 metadata from mp3 files for easy access after the files have been searched through. Okay, so how do you access this information, well it's all pretty simple really:
FileData FileData = (FileData)FileExplorer.FileList[0];
this.DisplayText.Text += "" + FileData.Length;
Code References
The test harness in the source files uses code from the article C# does Shell, Part 1 by Arik Poznanski to implement a dialog to select a folder. You would have thought the .NET Team would have added one in the Core NameSpace's wouldn't you. That would be too easy.
Conclusion
A quick and easy way to gather files from a computer into a collection that can be used in a larger application. The source files contain a class library project and a test harness project in a Visual Studio.NET 2002 solution. I just hope someone besides me can use it to speed up their development.