To use custom filter criteria in
FileSet derive from it and implement
IsMatch method yourself. Then use the derived class instead of the
Ftp client = new Ftp();
// connect, login, ...
// download files using MyFileSet
MyFileSet set = new MyFileSet("/data", @"^data[0-9]+\.txt$");
Here is a sample code for filtering using
/// Matches files in BasePath directory (no subdirectory is processed)
/// defined by a regular expression.
public class MyFileSet : FileSet
public MyFileSet(string basePath, string regexPattern)
_regexPattern = regexPattern;
public override bool IsMatch(string relativePath, FileSetMatchMode mode)
// implement behavior for each match mode
// take IsCaseSensitive into consideration
RegexOptions options = RegexOptions.None;
options = RegexOptions.IgnoreCase;
// include files matching the specified pattern
return Regex.IsMatch(relativePath, _regexPattern, options);
// don't match any directories (only files)
// traverse only current directory
return relativePath == ".";
throw new ArgumentOutOfRangeException("mode");
Please note, that this performs only SHALLOW SEARCH (no subdirectories are traversed). It is because in "
case FileSetMatchMode.TraverseDirectory:" true is returned only for "." path (current directory). To perform DEEP SEARCH (traverse also subdirectories) update the class as follows:
// traverse all directories and subdirectories
But please note, that this can be very time consuming. You should traverse only "meaningful" directories, however it depends on your application what "meaningful" means.
IMPLEMENTATION NOTE: for optimization issues when
IsMatch is called with parameters equal to "." and
FileSetMatchMode.TraverseDirectory the method should return false if no inclusion pattern was specified; true otherwise.
It means that if you return false always, current directory will not be traverses and no file or directory will be matched at all.