![]() |
Win32FileFinder |
|
| Home « Computers « Win32FileFinder | ||
The following notes are taken from the source code documentation of the Win32FileFinder class.
A utility class that wraps the Win32 file finding functions in a familar
and convenient managed way with callbacks while hiding the underlying
complexities of the API calls, handles and return codes. This class uses the FindFirstFile, FindNextFile and FindClose Win32 API methods internally to run a find, sending an event with managed FileInfo or DirectoryInfo objects as callbacks so that consumers can work with the results as they are found. The caller sequentially consumes the results and can cancel the find during a callback, which is not possible with the managed methods. The class does not yet recursively scan folders. This can be done, but it will be necessary to maintain a stack of the find handles. |
I created the Win32FileFinder class after years of dissatisfaction with the standard GetFiles and GetDirectories methods. As the documentation above says, the standard managed methods block while the result arrays are being generated and cannot be interrupted. The block can be hidden by running it in a thread, but this adds irritating code complexity. Even worse, there is no way of getting progress from the standard methods, nor is there a way of interrupting them.
The Win32FileFinder class uses Win32 API calls and handles internally to run the file find process and it feeds the results sequentially back to the caller via an event. The class hides the internal workings from the caller who only sees familiar managed FileInfo and DirectoryInfo objects passed back in the event. The find process can be cancelled by setting a flag in the event arguments. Beginner .NET developers might be interested in the Interop techniques used with the unmanaged API calls and structures. The class also implements to the IDisposable interface to ensure that the underlying HANDLE is eventually closed.
I tried to make the class as easy as possible to use. This is the skeleton code of how to use the class:
finder = new Win32FileFinder();
finder.FileFound += new FileFoundEventHandler(finder_FileFound);
finder.Find(@"C:\windows\system32\win*");
:
private void finder_FileFound(object sender, FileFoundEventArgs e)
{
if (e.File != null)
Console.WriteLine("Found file: {0}", e.File.FullName);
else
Console.WriteLine("Found folder: {0}", e.Folder.FullName);
// you could set e.Cancel=true here to stop finding
} |
Following is a sample of the output of the test driver console command. The 2nd parameter is the maximum number of find results to process.
>finder C:\windows\system32\e* 20 050214161047 020129143310 64.0 KB C:\windows\system32\EBAPI.dll
050120152453 020711160600 80.0 KB C:\windows\system32\EBPEPS02.DLL
050120152453 010903170400 182 bytes C:\windows\system32\EBPPORT.DAT
030331220000 030331220000 68.2 KB C:\windows\system32\edit.com
030331220000 030331220000 10.5 KB C:\windows\system32\edit.hlp
030331220000 030331220000 12.3 KB C:\windows\system32\edlin.exe
050214161047 021114124310 120 KB C:\windows\system32\EEBAPI.dll
050214161047 021114124314 100 KB C:\windows\system32\EEBDSCVR.dll
050214161047 010821010000 53.0 KB C:\windows\system32\EEBSDKIF.dll
050214161047 020110200532 64.0 KB C:\windows\system32\EEBUtil.dll
030331220000 040804175642 26.0 KB C:\windows\system32\efsadu.dll
030331220000 030331220000 124 KB C:\windows\system32\ega.cpi
030331220000 040804175642 179 KB C:\windows\system32\els.dll
040726135559 040726135559 21.1 KB C:\windows\system32\emptyregdb.dat
<DIR> C:\windows\system32\en-US
040726182706 040804175642 20.0 KB C:\windows\system32\encapi.dll
030331220000 040804175642 182 KB C:\windows\system32\encdec.dll
040726233206 030331220000 101 KB C:\windows\system32\EqnClass.Dll
030331220000 040804175642 22.5 KB C:\windows\system32\ersvc.dll
040726163444 050726143945 237 KB C:\windows\system32\es.dll
| finder_proj.zip - A zip of the complete Visual Studio 2005 project. Version 1.3.0.0 published on 25-Aug-2007 corrects a bug where Dispose fails if zero results were found in the search. | |
| Win32FileFinder.cs.txt - The C# source file of the file finder classes (renamed to txt). | |
| FinderCommand.cs.txt - The C# source file for the console command driver class (renamed to txt). |