How to use File Providers in ASP.NET Core

By FoxLearn 1/4/2025 3:23:55 AM   29
File Providers in ASP.NET Core offer an abstraction layer over the file system, enabling easy retrieval of file and directory information, monitoring for changes, and accessing physical files.

File Provider abstractions

In ASP.NET Core, the IFileProvider interface has three implementations: PhysicalFileProvider, EmbeddedFileProvider, and CompositeFileProvider.

  • PhysicalFileProvider: Provides access to the physical file system.
  • EmbeddedFileProvider: Accesses files embedded within assemblies.
  • CompositeFileProvider: Combines access from multiple providers through a single interface.

The IFileProvider interface exposes methods for retrieving file and directory information using IFileInfo and IDirectoryContents. It also supports change notifications through the IChangeToken interface.

Use PhysicalFileProvider in ASP.NET Core

Creating a new ASP.NET Core project, then inject a PhysicalFileProvider object as a dependency by using the ConfigureServices method in the Startup.cs file.

The process involves configuring the service in the ConfigureServices method and using the injected dependency in a controller:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    IFileProvider physicalProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
    services.AddSingleton<IFileProvider>(physicalProvider);
}

Next, in the controller (here, we’ll use ValuesController), the IFileProvider is injected via the constructor. The following code demonstrates how to access files from the current directory using IFileProvider:

public class ValuesController : ControllerBase
{
    private readonly IFileProvider _fileProvider;

    public ValuesController(IFileProvider fileProvider)
    {
        _fileProvider = fileProvider;
    }

    [HttpGet]
    public ActionResult<List<string>> Get()
    {
        List<string> fileList = new List<string>();
        var contents = _fileProvider.GetDirectoryContents("");
        foreach (IFileInfo fileInfo in contents)
        {
            fileList.Add(fileInfo.Name);
        }
        return fileList;
    }
}

In this example, the Get method retrieves the file names from the current directory using IFileProvider. The names are returned as a list of strings.

Using CompositeFileProvider in ASP.NET Core

The CompositeFileProvider combines multiple IFileProvider instances into a single interface.

Below is an example where both a physical file provider and an embedded file provider are combined:

var physicalProvider = _env.ContentRootFileProvider;
var embeddedProvider = new EmbeddedFileProvider(Assembly.GetEntryAssembly());
var compositeProvider = new CompositeFileProvider(physicalProvider, embeddedProvider);

Using EmbeddedFileProvider in ASP.NET Core

If you need to access files embedded in assemblies, the EmbeddedFileProvider is a useful class.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    IFileProvider embeddedProvider = new EmbeddedFileProvider(Assembly.GetEntryAssembly());
    services.AddSingleton<IFileProvider>(embeddedProvider);
}

With ASP.NET Core, you can use file providers like PhysicalFileProvider, EmbeddedFileProvider, and CompositeFileProvider to work with files and directories, and set up change notifications. Dependency injection allows you to easily manage these file providers in your application, injecting the necessary instance where needed.