Skip to content

IDirectory

rosaliafx edited this page Dec 3, 2015 · 6 revisions

IDirectory class members.

AbsolutePath

Gets directory "absolute" path. It is the path the directory was originaly created with:

IFile projectsDir = "C:/Projects";

string path = projectDir.AbsolutePath; // path is C:/Projects

...or the path derived from parent directory:

IDirectory myProjectsDir = "C:/Projects/MyProject"
IDirectory src = myProjectsDir/"Src";

string path = src.AbsolutePath; // path is C:/Projects/MyProject/Src

There is an implicit to string conversion operation that returns AbsolutePath so the call to this property could be omited:

IDirectory myProjectsDir = "C:/Projects/MyProject"
IDirectory src = myProjectsDir/"Src";

string path = src; // path is C:/Projects/MyProject/Src
Exists

Gets the flag indicating whether the directory exists.

IFile artifacts = "C:/Projects/MyProject/Artifacts";

bool artifactsExists = artifacts.Exists;
Name

Gets directory name without preceding path.

IFile artifacts = "C:/Projects/MyProject/Artifacts";

string dirName = artifacts.Name; // "Artifacts"
Parent

Gets parent directory object (IDirectory).

Directories

Gets IEnumerable of IDirectory objects representing childs of current directory.

Files

Gets FileList containing children files of current directory. Note that the list will contain only direct child files, not files from subdirectories. If you need all files from subdirectories use SearchFilesRecursively helper.

IDirectory artifacts = "C:/Projects/MyProject/Artifacts";

artifacts.Files.DeleteAll();                    // will delete direct children 
                                                // files from Artifacts directory

artifacts.SearchFilesRecursively().DeleteAll(); // will delete all files including 
                                                // subdirectories
EnsureExists()

Creates the directory if it does not exist.

IFile artifacts = "C:/Projects/MyProject/Artifacts";

// creates C:/Projects/MyProject/Artifacts (if it does not exist) directory
artifacts.EnsureExists(); 
Delete()

Deletes the directory.

GetRelativePath(IDirectory directory)

Calclulates relative path from provided directory to the current directory.

IFile file = "C:/A/B/file.txt";
IDirectory dir = "C:/A/D";

string relativeFilePath = file.GetRelativePath(dir); // "../B/file.txt"
GetDirectory(string name)

Gets a IDirectory object representing child of current dirrectory. It is functionally close to / operator, but the difference is that GetDirectory always returns IDirectory while / could return IFile:

IDirectory dir = "C:/Projects";
IDirectory myProject1 = dir.GetDirectory("MyProject");
IDirectory myProject2 = dir/"MyProject";

// myProject1 and myProject2 are the same directories
GetFile(string name)

Gets a IFile object representing child file of current dirrectory. Just like GetDirectory it could be used instead of / operator:

IDirectory dir = "C:/Projects";
IFile myProject1 = dir.GetFile("MyProject.csproj");
IFile myProject2 = dir/"MyProject.csproj";

// myProject1 and myProject2 are the same files