Skip to content
MH edited this page Dec 17, 2020 · 13 revisions

DirSearch is a C# Class Library for directory searching and filtering.

Notes:

  • Initial filtering is supported by using traditional wildcard-matching and optional file attributes.
  • File security is enforced as expected through .Net and Windows security.
  • Delegates (callbacks) can be used by the caller for every file, folder, and/or file-traversal exceptions. This can be used for progress indicators, logging, etc. Also, custom attribute filtering can be done in callbacks.
  • Added in version 2.3: Custom folder and file filtering ability by the caller.
  • Note: Reparse points on folders are always skipped.

For attribute filtering, the AttrSearchType enumeration and bitwise attributes passed to the constructor dictates the filtering used. AttrSearchType follows:

  1. ExactMatch: The EXACT combination of attributes on the file must match.
    One common example of misuse: when looking for hidden + system files and the file also has the archive attribute set, the file would be excluded. In this case, you would likely want to use AllMatchPlusAnyOthers instead.
  2. AllMatchPlusAnyOthers: All of the attributes must match. Any extra file attributes found are ignored and the file is considered matching.
  3. AnyMatch: (default) Any of the attributes may match. You just need one or more matches.
  4. IgnoreAttributeMatch: Do not perform attribute-matching. Typically used with custom attribute-filtering. When using this option, the FileAttributes parameter is ignored.

Typical Use:

   // Get C# files needing backup
   var ds = new DirSearch("*.cs", @"E:\", AttrSearchType.AnyMatch, FileAttributes.Archive, true);
   ds.OnFileMatch += new DirSearch.FileMatch(MyOnFileMatch);
   ds.OnFolderMatch += new DirSearch.FolderMatch(MyOnFolderMatch);
   ds.OnFileExcept += new DirSearch.FileExcept(MyFileExcept);
   ds.OnFolderExcept += new DirSearch.FolderExcept(MyFolderExcept);
   ds.Execute();
   ds.OnFileMatch -= MyOnFileMatch;
   ds.OnFolderMatch -= MyOnFolderMatch;
   ds.OnFileExcept -= MyFileExcept;
   ds.OnFolderExcept -= MyFolderExcept;

   private void MyFolderExcept(string errorMsg) {
      logger.AppendLine(errorMsg);
   }
   private void MyFileExcept(string errorMsg) {
      logger.AppendLine(errorMsg);
   }

   private void MyOnFolderMatch(DirectoryInfo oneFolder, ref bool cancelFlag) {
      // code to handle folders encountered; this can be used for progress reporting
      // or skipped entirely
      // cancelFlag can be set in callback to cancel searching
   }

   private void MyOnFileMatch(FileInfo oneFile, ref bool cancelFlag) {
      // code to handle file matching
      // cancelFlag can be set in callback to cancel searching
   }

Currently targets .Net Standard 2.0 and .Net Framework 4.6.1. To use the NuGet package, refer to: OCSS.Util.DirSearch

Clone this wiki locally