• Lexical
Show / Hide Table of Contents
  • Lexical.FileSystem
    • Introduction
    • Abstractions
      • IFileSystem
        • IFileSystem
        • IFileSystemBrowse
        • IFileSystemCreateDirectory
        • IFileSystemDelete
        • IFileSystemFileAttribute
        • IFileSystemMount
        • IFileSystemMove
        • IFileSystemObserve
        • IFileSystemOpen
      • IEvent
      • IEntry
      • IOption
      • IToken
    • FileSystem
    • VirtualFileSystem
    • MemoryFileSystem
    • EmbeddedFileSystem
    • HttpFileSystem
    • Decoration
    • IFileProvider
    • Utility
      • DisposeList
      • FileScanner
      • VisitTree
      • File Operation
  • Lexical.FileProvider
    • Introduction
    • Package
    • Package.Abstractions
    • Root
    • Zip
    • Dll
    • SharpCompress
    • SharpZipLib
    • FileScanner
    • Utils
  • Lexical.Localization
    • Introduction
    • Tutorial
    • Asset
      • IAsset
      • IStringAsset
    • Line
      • ILine
      • ILineFactory
      • ILineRoot
      • ILineFormat
      • ILineLogger
      • LineComparer
    • File
      • ILineReader
      • ILineWriter
      • Ini
      • Json
      • Xml
      • Resx
      • Resources
    • Miscellaneous
      • Plurality
      • ICulturePolicy
      • IStringFormat
      • Dependency Injection
    • Practices
      • Class Library
      • Class Library DI
      • Class Library DI opt.
  • Lexical.Utilities
    • Introduction
    • UnicodeString
    • FileScanner
    • Permutation
    • Tuples
    • StructList

VisitTree

The extension method IFileSystem.VisitTree(string path, int depth) visits a tree structure of a filesystem.

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");

foreach (TreeVisit.Line line in ram.VisitTree())
{
    Console.WriteLine(line);
}
""
├── ""
│  ├── "mnt"
│  ├── "tmp"
│  │  └── "helloworld.txt"
│  └── "usr"
│     └── "lex"
├── "c:"
│  └── "dir"
│     └── "dir"
└── "file:"
   └── ""
      └── "c:"
         └── "temp"

Parameter depth determines visit depth.

foreach (TreeVisit.Line line in ram.VisitTree(depth: 1))
{
    Console.WriteLine(line);
}
""
├── ""
├── "c:"
└── "file:"

Parameter path determines start location.

foreach (TreeVisit.Line line in ram.VisitTree(path: "/tmp/"))
{
    Console.WriteLine(line);
}
"tmp"
└── "helloworld.txt"

PrintTree

The extension method IFileSystem.PrintTo(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.PrintTo(Console.Out);
""
├── ""
│  ├── "mnt"
│  ├── "tmp"
│  │  └── "helloworld.txt"
│  └── "usr"
│     └── "lex"
├── "c:"
│  └── "dir"
│     └── "dir"
└── "file:"
   └── ""
      └── "c:"
         └── "temp"

IFileSystem.PrintTo(TextWriter output, string path, int depth, Format format) appends to a StringBuilder.

StringBuilder sb = new StringBuilder();
ram.PrintTo(sb);

IFileSystem.Print(string path, int depth, Format format) prints out to as string.

Console.WriteLine(ram.Print());

Parameter depth determines visit depth.

Console.WriteLine(ram.Print(depth:1));
""
├── ""
├── "c:"
└── "file:"

Parameter path determines start location.

Console.WriteLine(ram.Print(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.Print(format: PrintTree.Format.Tree | PrintTree.Format.Path | 
                                 PrintTree.Format.Length | PrintTree.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).
Back to top Copyright © 2015-2020 Toni Kalajainen