How does .NET5 embed static files in DLLs, and how to use the files embedded in DLLs with the host program?
TinyVFS is a virtual file system, inspired by the ABP vNext framework. It can embed js, CSS, image, cshtml, and other files into the assembly, and they can be used like physical files at runtime.
Features
- In a single application, it can divide the front end and back end (management system) into separate projects.
- It allows developers to develop different businesses or modules at the same time.
- It allows us to split the system function modules and assemble them.
Quick Start
Install-Package
Install-Package TinyVFS
Registering Embedded Files
Edit web resource project .csproj
- <ItemGroup>
- <EmbeddedResource Include="MyResources\**\*.*" />
- <Content Remove="MyResources\**\*.*" />
- </ItemGroup>
Embed the file into the virtual file system with the following code snippet,
- services.AddVirtualFilesService();
- services.Configure < VirtualFileSystemOptions > (options => {
- options.FileSets.AddEmbedded < WebApplication1.Pages.IndexModel > ("WebResources");
- });
Getting Virtual Files: IVirtualFileProvider
After embedding into the assembly, you can obtain the file or directory content through IVirtualFileProvider
- public class MyService {
- private readonly IVirtualFileProvider _virtualFileProvider;
- public MyService(IVirtualFileProvider virtualFileProvider) {
- _virtualFileProvider = virtualFileProvider;
- }
- public void Foo() {
- //Getting a single file
- var file = _virtualFileProvider.GetFileInfo("/MyResources/js/test.js");
- var fileContent = file.ReadAsString(); //ReadAsString is an extension method of ABP
- //Getting all files/directories under a directory
- var directoryContents = _virtualFileProvider.GetDirectoryContents("/MyResources/js");
- }
- }

Join the conversation! Your thoughts help the community grow.