PrintTree
The extension method IFileSystem.PrintTreeTo(TextWriter output, string path, int depth, Format format) prints a tree structure to a TextWriter such as Console.Out (stdout).
IFileSystem ram = new MemoryFileSystem();
ram.CreateDirectory("/tmp/");
ram.CreateDirectory("/mnt/");
ram.CreateDirectory("/usr/lex/");
ram.CreateDirectory("c:/dir/dir/");
ram.CreateFile("/tmp/helloworld.txt", Encoding.UTF8.GetBytes("Hello World!\r\n"));
ram.CreateDirectory("file://c:/temp/");
ram.PrintTreeTo(Console.Out);
"" ├── "" │ ├── "mnt" │ ├── "tmp" │ │ └── "helloworld.txt" │ └── "usr" │ └── "lex" ├── "c:" │ └── "dir" │ └── "dir" └── "file:" └── "" └── "c:" └── "temp"
IFileSystem.PrintTreeTo(TextWriter output, string path, int depth, Format format) appends to a StringBuilder.
StringBuilder sb = new StringBuilder();
ram.PrintTreeTo(sb);
IFileSystem.PrintTree(string path, int depth, Format format) prints out to as string.
Console.WriteLine(ram.PrintTree());
Parameter depth determines visit depth.
Console.WriteLine(ram.PrintTree(depth: 1));
"" ├── "" ├── "c:" └── "file:"
Parameter path determines start location.
Console.WriteLine(ram.PrintTree(path: "/tmp/"));
"tmp" └── "helloworld.txt"
Parameter format determines which infos are printed out. For example PrintTree.Format.Path prints out full path instead of name.
string tree = ram.PrintTree(format: FileSystemPrintTreeExtensions.Format.Tree | FileSystemPrintTreeExtensions.Format.Path |
FileSystemPrintTreeExtensions.Format.Length | FileSystemPrintTreeExtensions.Format.Error);
├── / │ ├── /mnt/ │ ├── /tmp/ │ │ └── /tmp/helloworld.txt 14 │ └── /usr/ │ └── /usr/lex/ ├── c:/ │ └── c:/dir/ │ └── c:/dir/dir/ └── file:/ └── file:// └── file://c:/ └── file://c:/temp/
PrintTree.Format flags:
Flag | Description |
---|---|
PrintTree.Format.Tree | The tree structure. |
PrintTree.Format.Name | Entry name. |
PrintTree.Format.Path | Entry path. |
PrintTree.Format.Length | Entry length for file entires. |
PrintTree.Format.Error | Traverse error. |
PrintTree.Format.LineFeed | Next line. |
PrintTree.Format.Mount | Mounted filesystem. |
PrintTree.Format.DriveLabel | Label of filesystem drive or volume. |
PrintTree.Format.DriveFreespace | Available free space on the drive or volume. |
PrintTree.Format.DriveSize | Total size of drive or volume. |
PrintTree.Format.DriveType | Drive or volume type, such as: Fixed, Removeable, Network, Ram |
PrintTree.Format.DriveFormat | FileSystem format, such as: NTFS, FAT32, EXT4 |
PrintTree.Format.FileAttributes | File attributes, such as: ReadOnly, Hidden, System, Directory, Archive |
PrintTree.Format.PhysicalPath | Physical path |
PrintTree.Format.Default | PrintTree.Format.Tree | PrintTree.Format.Name | PrintTree.Format.Error |
PrintTree.Format.DefaultPath | PrintTree.Format.Tree | PrintTree.Format.Path | PrintTree.Format.Error |
PrintTree.Format.All | All flags. |
PrintTree.Format.AllWithName | All flags with name printing (excludes path printing). |
PrintTree.Format.AllWithPath | All flags with path printing (excludes name printing). |