Managed Extensions: Finding Files by Extension

Welcome to this week's installment of .NET Tips & Techniques! Each week, award-winning Architect and Lead Programmer Tom Archer demonstrates how to perform a practical .NET programming task using either C# or Managed C++ Extensions.

A reader recently asked me how to locate all files with a specific extension so that he could register those files with his application. Basically, all he needs to do is allow the user to specify where he wants the search to begin (accomplished with the .NET FolderBrowserDialog component) and then have a recursive function enumerate each folder and compare the searched-for file extension with the extension of each file found. The "matching" files are returned to the caller in an ArrayList object. Figure 1 shows an example of a demo application I wrote to illustrate this:

Folder-Browser Dialogs in .NET

Many C++ classes today allow you to display a folder-browser dialog. However, if you had to perform this task (using the Shell API function SHBrowseForFolder) before C++ helper classes and better documentation existed, you'll be happy to note that this task is much easier in .NET (specifically, Windows Forms applications). For this article's demo, I did the following:

I opened the form whose class will use the folder browser.

I opened the Toolbox and dragged a FolderBrowserDialog control onto the form. (I positioned it below the form because it's a class and not a UI element.)

I specified the needed properties. IntelliSense helped here, but the most commonly specified ones were things like Description (the text that will display on the dialog above the folder list), ShowNewFolderButton (a property that allows you to specify if you want the user to be able to create folders while browsing), and RootFolder (whose value is used to specify the top-level folder that the user cannot exceed). The demo's Form::Load method has the following initialization:

Searching for Files

Once I had the basic demo UI in place, I wrote a very simple class (FileSearcher) that implements a single static method (FindByExtension) to allow a caller to search for files in a specified folder (and optionally, its subfolders). Here are the basic steps that accomplished the task:

Call the System::IO::Directory::GetFiles method to retrieve all files for a specified folder. The files' names are returned in an ArrayList object where each file is a String object.

Enumerate the returned ArrayList object by using a for loop.

For each String (file name) in the ArrayList, call the Path::GetExtension method to return the file name's extension and then compare it (ignoring case) to the caller-specified extension. If the strings match, the file name is added to the caller's passed ArrayList object.

Once the current folder has been enumerated, the method checks the Search-Subfolders flag. If it is off, the method returns. If it is on, the method calls the System::IO::Directory::GetDirectories method to retrieve all folders for the current folder. These folders are then enumerated and the FileSearcher::FindByExtension method calls itself for each found folder.

Areas That Can Be Improved

While this article has illustrated how to display a folder-browser dialog and how to search a user's folder(s) for files matching a specified extension, the solution has plenty of room for improvement. (Isn't there always?) Mainly, the FileSearcher class could be augmented to support multiple extensions, as well as asynchronous delegates and events to notify the caller of progress. That way, the caller isn't blocked on the call to the FindByExtension method and can display a progress bar, as well as allow the user to cancel what could be a very lengthy operation.

Not only would that flesh out the FileSearcher class, making it more usable, but it would also represent a great example of where and how asynchronous delegates and events can be used. Depending on the feedback I get from this article, I'll do that article at some time in the future.

About the Author

Tom Archer - MSFT

I am a Program Manager and Content Strategist for the Microsoft MSDN Online team managing the Windows Vista and Visual C++ developer centers.
Before being employed at Microsoft, I was awarded MVP status for the Visual C++ product. A 20+ year veteran of programming with various languages - C++, C, Assembler, RPG III/400, PL/I, etc. - I've also written many technical books (Inside C#, Extending MFC Applications with the .NET Framework, Visual C++.NET Bible, etc.) and 100+ online articles.

Top White Papers and Webcasts

U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.

When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.