• 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.FileProvider.Dll

DllFileProvider.OpenFile(string) opens .dll files. This library uses Mono.Cecil NuGet package.

DllFileProvider fileProvider = DllFileProvider.OpenFile("Lexical.Localization.Tests.dll");

Another constructor is DllFileProvider.UseStream(Stream).

// Open some stream
Stream stream = new FileStream("Lexical.Localization.Tests.dll", FileMode.Open);

// Use stream as dll file.
DllFileProvider fileProvider = DllFileProvider.UseStream(stream).AddDisposable(stream);

List all embedded resources.

foreach (string embeddedResource in fileProvider.ListAllPaths().OrderBy(s=>s))
    Console.WriteLine(embeddedResource);

Looks like this.

Lexical.Localization.Tests.localization.ini
Lexical.Localization.Tests.localization.json
Lexical.Localization.Tests.Localization.localization.ini
Lexical.Localization.Tests.Localization.localization-en.ini
Lexical.Localization.Tests.Localization.localization-fi.ini
...

File provider must be disposed after use.

fileProvider.Dispose();

Belated disposing can be added to the file provider. The disposable will be disposed once the fileprovider and all its streams are closed.

// Create file provider
DllFileProvider fileProvider = DllFileProvider.OpenFile("Lexical.Localization.Tests.dll");
// Add disposable for belated dispose
fileProvider.AddBelatedDispose(new _Disposable_());
// Open stream
Stream s = fileProvider
        .GetFileInfo("Lexical.Localization.Tests.Localization.localization-en.ini")
        .CreateReadStream();
// Dispose file provider
fileProvider.Dispose();
// Dispose the open stream  --  _Disposable_ is disposed here.
s.Dispose();

Package Loader

Lexical.FileProvider.PackageLoader.Dll can be used as a component of PackageFileLoader, making it possible to drill down into embedded resources of .dll files. Lexical.FileProvider.PackageLoader.Exe can open embedded resources of managed .exe files.

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

// Create package options
IPackageFileProviderOptions options =
    new PackageFileProviderOptions()
    .AddPackageLoaders(
        Lexical.FileProvider.PackageLoader.Dll.Singleton, 
        Lexical.FileProvider.PackageLoader.Exe.Singleton
    );

// Create package file provider
IPackageFileProvider fileProvider = new PackageFileProvider(root, options).AddDisposable(root);

Links

  • Lexical.FileProvider.Dll (NuGet)
  • Lexical.FileProvider.Abstractions (NuGet)
    • IPackageLoader
  • Microsoft.Extensions.FileProviders.Abstractions (NuGet)
    • IFileProvider
  • Mono.Cecil (NuGet)
Back to top Copyright © 2015-2020 Toni Kalajainen