Skip to content

FileList

guryanovev edited this page Dec 15, 2015 · 9 revisions

FileList is a special class that encapsulates

  • a list of files (IFile objects);
  • a base directory the files was read from.

The main purposes of this class are

  • to provide handy way of performing recursive operations with file tree;
  • to support file selection (include/exclude).

Instantiating FileList

You would probably get a FileList from a directory. And there are two ways to do that, it is important to understand the difference between them. First way is to use Files property of the directory:

IDirectory src = "C:/Projects/MyProject/Src";
FileList files = src.Files; // files contain only direct Src children

In this case the files will contain only direct children files from Src directory not from sub-directories. It means that operations like CopyTo will not work recursively because the initial FileList does not contain recursive children.

The second way is using SearchFilesRecursively() extension method. This metod will fetch direct children and all files from all sub-directories:

IDirectory src = "C:/Projects/MyProject/Src";
FileList files = src.SearchFilesRecursively(); 
// files contain all children files including subdirectories

Filtering Files

FileList has a number of method to include or exclude files from the list:

IDirectory src = "C:/Projects/MyProject/Src";
FileList allBinaries = src
    .SearchFilesRecursively()
    .IncludeByExtension("dll"); 

FileList Operations

DeleteAll()

Removes all files

CopyAllTo(IDirectory directpry)

Copies all files to directory as a flat list

CopyRelativelyTo(IDirectory directory)

Copies all files to provided directory preserving all subdirectories from relative path.