Skip to content

File System

guryanovev edited this page Dec 10, 2015 · 42 revisions

Working with files is a common task for a typical build script. That's why Rosalia has special set of helper classes to make work with File system more comfortable.

Basics

Two classes, IFile and IDirectory forms the core or Rosalia's FS library. They intended to incapsulate information for a file or a directory respectively.

Getting started with FS

The starting point is getting an instance of IFile or IDirectory that represents your File System. Both IFile and IDirectory could be instantiated by implicit conversion from string representing the path:

IDirectory src = "C:/Projects/MyProject/Src";
IFile solutionFile = "C:/Projects/MyProject/Src/MyProject.sln";

When you work with Rosalia's Workflow you already have an instance of IDirectory that points to the work directory:

protected override void RegisterTasks()
{
    /*...*/
    IDirectory workDirectory = this.WorkDirectory;
    // work with work directory
    /*...*/
}

Traversing FS

Once you have an instance of IDirectory, you can access child directories with / operator:

IDirectory src = WorkDirectory/"Src";

The same applies to files -- you can access a file from parent directory using / operator:

IDirectory src = WorkDirectory/"Src";
IFile solutionFile = WorkDirectory/"Src"/"MyProject.sln";

/ operator is a handy equivalent of Path.Combine and it's preferred to use this operator instead of hard-coding a separator character ('/' or ''):

IFile solutionFile = WorkDirectory/"Src"/"MyProject.sln"; // Good
IFile solutionFile = WorkDirectory/"Src/MyProject.sln"; // Bad

Typical File System operations

Once you've got an instance of IFile or IDirectory, you can use it to achieve your actual File-System-related tasks. Both IFile and IDirectory classes have a set of methods for typical FS operations. Please see IFile members and IDirectory members for details.

FileList

While IFile and IDirectory classes form the basis to work with FS, there is one more important class called FileList. This class holds not only the list of files but the base directory that could be used to build relative path for recursive operations. See FileList for details.

Extension Methods

IFile Extensions

WriteStringToFile(string content)

Writes string to file.

IFile myfile = "C:/Data/myfile.txt";
myfile.WriteStringToFile("Hello World!");
ReadAllText()

Reads string content from file.

IFile myfile = "C:/Data/myfile.txt";
string content = myfile.ReadAllText(); // "Hello World"

IDirectory Extensions

SearchFilesRecursively()

Returns FileList that contains all files within the directory (including subdirectories). This method is used when you want to do recursive operations with files (copy a directory, zip it all pack to NuGet package etc). See FileList for details.

GetParentsChain()

Returns an IEnumerable<IDirectory> that contains the directory itself and all it's parents up to the tree. It might be useful if you are searching for a particular parent directory.

IDirectory dir = "C:/A/B/C";
IDirectory[] parents = dir.GetParentsChain().ToArray(); // ["C:/A/B/C", "C:/A/B", "C:/A", "C:/"]
Closest(Predicate<IDirectory> predicate)

Returns a closest directory (from GetParentsChain() list) that matches provided condition.

IDirectory dir = "C:/Projects/MyProject/Src/Web/bin/Debug";
IDirectory src = dir.Closest(d => d.Name == "Src"); // "C:/Projects/MyProject/Src"
Clone this wiki locally