FileMask ClassCleanCode C# Libraries v1.2.03 API
Provides a simple control to filter a directory by specifying one--or more--file masks displaying both the list of matching files and a count of matched files; it additionally allows customizing this generic control with additional arbitrary criteria.
Inheritance Hierarchy

OnlineSystem Object
  OnlineSystem MarshalByRefObject
    OnlineSystem.ComponentModel Component
      OnlineSystem.Windows.Forms Control
        OnlineSystem.Windows.Forms ScrollableControl
          OnlineSystem.Windows.Forms ContainerControl
            OnlineSystem.Windows.Forms UserControl
              CleanCode.GeneralComponents.Controls FileMask

Namespace: CleanCode.GeneralComponents.Controls
Assembly: CleanCode.GeneralComponents (in CleanCode.GeneralComponents.dll) Version: (1.2.03)

public class FileMask : UserControl

Using this control takes just a few lines of code. First, either in your form constructor, or at another appropriate point, you must initialize the SourceDirectory and Mask properties. The Mask property setting in the example illustrates that you may specify more than one mask, separating them by commas, spaces, or vertical bars. The RestrictionLambda is optional; with it you can specify additional filtering criteria beyond a simple name match of file masks. The RestrictionLambda function shown in the example here selects for files after a certain date.

fileMask.SourceDirectory = @"C:\some\path\here";
fileMask.Mask = "*.txt|*.csv";
fileMask.RestrictionLambda =
    f => DateTime.Compare(File.GetLastWriteTime(f), MyEarliestDateAllowed) >= 0;

Next, you must ensure that the specified masks for the specified directory are accurately reflected in the control by calling the UpdateFileMatches  method at an appropropriate point. If you place the control on your main form, that point could be during initialization. If you choose instead to use the control on a subform (where it will not always be visible) use something like this to update it when it is exposed:

protected override void OnVisibleChanged(EventArgs e)
    if (Visible)

Finally, you need to use the output of the control, residing in the FileList property:

foreach (string filename in FileList)
    // process the file here...

See my article on the Simple-Talk online magazine discussing the design and use of the FileMask entitled Online Using LINQ Lambda Expressions to Design Customizable Generic Components.

Since CleanCode 0.9.31.

See Also