• 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

Lexical.FileScanner

Lexical.FileProviders.FileScanner is a configurable scanner that searches for files and folders. It is configurable to match file paths against wildcards, regular expressions, and glob patterns. It uses all threads concurrently, which makes it effective for opening package files.

// Create root file provider
PhysicalFileProvider root = new PhysicalFileProvider(Directory.GetCurrentDirectory());

// Create package options
IPackageFileProviderOptions options =
    new PackageFileProviderOptions()
    .AddPackageLoaders(Dll.Singleton, Exe.Singleton, Zip.Singleton, Rar.Singleton, _7z.Singleton,
                       Tar.Singleton, GZip.Singleton, BZip2.Singleton, Lzw.Singleton);

// Create package file provider
using (var fileProvider = new PackageFileProvider(root, options).AddDisposable(root))
{
    // Glob scan with multiple threads
    string[] files = new FileScanner(fileProvider)
        .AddGlobPattern("**.zip/**.resources")
        .OrderBy(s => s)
        .ToArray();

    // Print
    foreach (String filename in files)
        Console.WriteLine(filename);
}

Errors that are thrown by file provider are captured. .SetErrorTarget(IProducerConsumerCollection<Exception>) sets a target where captured errors are to be placed.

int count = 0;
ConcurrentBag<Exception> errors = new ConcurrentBag<Exception>();
foreach (string filepath in new FileScanner(fileProvider).AddGlobPattern("**").SetErrorTarget(errors))
{
    // Print errors
    Exception e;
    if (errors.TryTake(out e)) Console.Error.WriteLine(e);

    Console.WriteLine(filepath);
    if (++count == 100) break;
}
fileProvider.Dispose();
Back to top Copyright © 2015-2020 Toni Kalajainen