http://www.developer.com/net/cplus/article.php/3412361/Managed-Extensions-Finding-Files-by-Extension.htm
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:
Figure 2 shows the result.
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.
To download the accompanying source code for this article, click here. The founder of the Archer Consulting Group (ACG), Tom Archer has been the project lead on three award-winning applications and is a best-selling author of 10 programming books as well as countless magazine and online articles.
Managed Extensions: Finding Files by Extension
September 24, 2004
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 attached demo, I did the following:
private: System::Void Form1_Load(System::Object * sender,
System::EventArgs * e)
{
...
browser->Description = S"Select the directory that you want to use as
the starting point for the search";
browser->ShowNewFolderButton = false;
browser->RootFolder = Environment::SpecialFolder::Desktop;
...
private: System::Void folderBrowserButton_Click(System::Object * sender, System::EventArgs * e)
{
System::Windows::Forms::DialogResult result = browser->ShowDialog();
if (DialogResult::OK == result)
startingFolder->Text = browser->SelectedPath;
}
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:
Here's the code from the attached demo's Form1.h file:
__gc class FileSearcher
{
public:
static void FindByExtension(String* startingFolder,
String* extension,
ArrayList* matchingFiles,
bool recurseSubFolders)
{
String* fileList[] = Directory::GetFiles(startingFolder);
String* fileName;
// search current folder
for (int i = 0; i < fileList->Count; i++)
{
fileName = static_cast<String*>(fileList->Item[i]);
if (0 == String::Compare(Path::GetExtension(fileName),
String::Format(S"{0}{1}",
extension->StartsWith(S".") ? S"" : S".",
extension),
true))
matchingFiles->Add(fileName);
}
if (recurseSubFolders)
{
// for each subfolder
fileList = Directory::GetDirectories(startingFolder);
for (int i = 0; i < fileList->Count; i++)
{
FindByExtension(static_cast<String*>(fileList->Item[i]),
extension,
matchingFiles,
true);
}
}
}
};
The client then uses the FileSearcher class like this (where the value being passed to the FindByExtension method is being retrieved from the form):
ArrayList* matches = new ArrayList();
FileSearcher::FindByExtension(startingFolder->Text,
extensionToFind->Text,
matches,
searchSubfolders->Checked);
listBox1->Items->Clear();
for (int i = 0; i < matches->Count; i++)
{
listBox1->Items->Add(matches->Item[i]);
}
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.
Download the Code
About the Author