System MarshalByRefObject
System.ComponentModel Component
System.Windows.Forms Control
System.Windows.Forms ScrollableControl
System.Windows.Forms ContainerControl
System.Windows.Forms UserControl
CleanCode.GeneralComponents.Controls FileMask
Namespace: CleanCode.GeneralComponents.Controls
Assembly: CleanCode.GeneralComponents (in CleanCode.GeneralComponents.dll) Version: 1.2.3.0 (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) { fileMask.UpdateFileMatches(); } base.OnVisibleChanged(e); }
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 Using LINQ Lambda Expressions to Design Customizable Generic Components.
Since CleanCode 0.9.31.