Advanced - File providers

In this article

File Provider interfaces

File Provider implementations

Implementation Description
Composite File Provider Used to provide combined access to files and directories from one or more other providers.
Manifest Embedded File Provider Used to access files embedded in assemblies.
Physical File Provider Used to access the system's physical files.

Physical File Provider

var provider = new PhysicalFileProvider(applicationRoot);
var contents = provider.GetDirectoryContents(string.Empty);
var filePath = Path.Combine("wwwroot", "js", "site.js");
var fileInfo = provider.GetFileInfo(filePath);
var physicalProvider = _env.ContentRootFileProvider;

Manifest Embedded File Provider

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="3.1.0" />
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Include="Resource.txt" />
  </ItemGroup>

</Project>
var manifestEmbeddedProvider = 
    new ManifestEmbeddedFileProvider(typeof(Program).Assembly);
Overload Description
ManifestEmbeddedFileProvider(Assembly, String) Accepts an optional root relative path parameter. Specify the root to scope calls to GetDirectoryContents to those resources under the provided path.
ManifestEmbeddedFileProvider(Assembly, String, DateTimeOffset) Accepts an optional root relative path parameter and a lastModified date (DateTimeOffset) parameter. The lastModified date scopes the last modification date for the IFileInfo instances returned by the IFileProvider.
ManifestEmbeddedFileProvider(Assembly, String, String, DateTimeOffset) Accepts an optional root relative path, lastModified date, and manifestName parameters. The manifestName represents the name of the embedded resource containing the manifest.

Composite File Provider

var physicalProvider = _env.ContentRootFileProvider;
var manifestEmbeddedProvider = 
    new ManifestEmbeddedFileProvider(typeof(Program).Assembly);
var compositeProvider = 
    new CompositeFileProvider(physicalProvider, manifestEmbeddedProvider);

services.AddSingleton<IFileProvider>(compositeProvider);

Watch for changes

private static readonly string _fileFilter = Path.Combine("TextFiles", "*.txt");

public static void Main(string[] args)
{
    Console.WriteLine($"Monitoring for changes with filter '{_fileFilter}' (Ctrl + C to quit)...");

    while (true)
    {
        MainAsync().GetAwaiter().GetResult();
    }
}

private static async Task MainAsync()
{
    var fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
    IChangeToken token = fileProvider.Watch(_fileFilter);
    var tcs = new TaskCompletionSource<object>();

    token.RegisterChangeCallback(state =>
        ((TaskCompletionSource<object>)state).TrySetResult(null), tcs);

    await tcs.Task.ConfigureAwait(false);

    Console.WriteLine("file changed");
}

Glob patterns

Pattern Description
directory/file.txt Matches a specific file in a specific ```directory```.
directory/*.txt Matches all files with .txt extension in a specific ```directory```.
directory/*/appsettings.json Matches all appsettings.json files in directories exactly one level below the directory folder.
directory/**/*.txt Matches all files with a .txt extension found anywhere under the directory folder.

Ref: File Providers in ASP.NET Core