public static IEnumerable<NativeMethods.FileFindData> FindFiles(String sDirectory, EntryTestRoutine recurseTest, EntryTestRoutine filterTest) { recurseTest = recurseTest ?? RecursionTest; filterTest = filterTest ?? ((ds, fp) => FitsMask(ds, "*")); String usemask = Path.Combine(sDirectory, "*"); foreach (var iterate in FindFiles(usemask)) { if (((FileAttributes) iterate.dwFileAttributes).HasFlag(FileAttributes.Directory)) { String subdir = Path.Combine(sDirectory, iterate.Filename); if (recurseTest(iterate.Filename, subdir)) { //recursive call. foreach (var subiterate in FindFiles(subdir, recurseTest, filterTest)) { var copied = subiterate; copied.FullFileName = Path.Combine(subdir, subiterate.FullFileName); yield return copied; } } } else { String fullpath = Path.Combine(sDirectory, iterate.Filename); if (filterTest(iterate.Filename, fullpath)) { NativeMethods.FileFindData ffd = new NativeMethods.FileFindData(); ffd.APIStructure = iterate; ffd.FullFileName = fullpath; yield return ffd; } } } }
public static IEnumerable<NativeMethods.FileFindData> FindFilesAsync(String sDirectory, EntryTestRoutine recurseTest, EntryTestRoutine filterTest) { recurseTest = recurseTest ?? RecursionTest; filterTest = filterTest ?? ((ds, fp) => FitsMask(ds, "*")); String usemask = Path.Combine(sDirectory, "*"); var gotresult = FindFilesAsync(usemask); var enumerator = gotresult.GetEnumerator(); Task<bool> asyncresult; (asyncresult = enumerator.MoveNext()).Wait(); var iterate = enumerator.Current; bool docontinue = false; do { iterate = enumerator.Current; var AdvanceResult = enumerator.MoveNext(); //AdvanceResult.Start(); if (((FileAttributes) iterate.dwFileAttributes).HasFlag(FileAttributes.Directory)) { String subdir = Path.Combine(sDirectory, iterate.Filename); if (recurseTest(iterate.Filename, subdir)) { //recursive call. foreach (var subiterate in FindFilesAsync(subdir, recurseTest, filterTest)) { var copied = subiterate; copied.FullFileName = Path.Combine(subdir, subiterate.FullFileName); yield return copied; } } } else { String fullpath = Path.Combine(sDirectory, iterate.Filename); if (filterTest(iterate.Filename, fullpath)) { NativeMethods.FileFindData ffd = new NativeMethods.FileFindData(); ffd.APIStructure = iterate; ffd.FullFileName = fullpath; yield return ffd; } } AdvanceResult.Wait(); docontinue = AdvanceResult.Result; } while (docontinue); }