ASP.NET Core 2.0 local file operation problems and Solutions

Time:2020-11-19

problem

How to ASP.NET Limited access to local directory and file information in core 2.0?

answer

Create an empty project, modify the startup class, and add the services needed to access local files


public void ConfigureServices(IServiceCollection services)
{
 services.AddSingleton<IFileProvider>(
  new PhysicalFileProvider(Directory.GetCurrentDirectory()));
}

Create a middleware, read all the files in the root directory, and output the file name:


public class HelloFileProviderMiddleware
{
 private readonly RequestDelegate _next;
 private readonly IFileProvider _fileProvider;
 public HelloFileProviderMiddleware(RequestDelegate next, IFileProvider fileProvider)
 {
  _next = next;
  _fileProvider = fileProvider;
 }
 public async Task Invoke(HttpContext context)
 {
  var output = new StringBuilder("");
  IDirectoryContents dir = _fileProvider.GetDirectoryContents("");
  foreach (IFileInfo item in dir)
  {
   output.AppendLine(item.Name);
  }
  await context.Response.WriteAsync(output.ToString());
 }
}
public static class UseHelloFileProviderExtensions
{
 public static IApplicationBuilder UseHelloFileProvider(this IApplicationBuilder app)
 {
  return app.UseMiddleware<HelloFileProviderMiddleware>();
 }
}

Run, the page effect:

Of course, we can also read the information of a single file through the ifileprovider interface


public class HelloFileProviderMiddleware
{
 private readonly RequestDelegate _next;
 private readonly IFileProvider _fileProvider;
 public HelloFileProviderMiddleware(RequestDelegate next, IFileProvider fileProvider)
 {
  _next = next;
  _fileProvider = fileProvider;
 }
 public async Task Invoke(HttpContext context)
 {
  IFileInfo file = _fileProvider.GetFileInfo("Startup.cs");
  using (var stream = file.CreateReadStream())
  {
   using (var reader = new StreamReader(stream))
   {
    var output = await reader.ReadToEndAsync();
    await context.Response.WriteAsync(output.ToString());
   }
  }
 }
}

Run, the page effect:

discuss

ASP.NET Core 2.0 provides a physicalfileprovider type inherited from the interface ifileprovider for restricted access to the local file system System.IO.File A package of.

We can configure ifileprovider as a service in the configure() method of startup, and then add it to middleware or controller through constructor injection. This allows you to control the file access path in one place (that is, when the application starts).

Ifileprovider has two important methods:

1. Getdirectorycontents: return idirectorycontents interface. Can be used to traverse all files or directories in a directory.

2. Getfileinfo: return ifileinfo interface. The contents of the file can be read through its createreadsteam method.

====start by sanshi=========================

After traversing the following directory, modify the root file:


public class HelloFileProviderMiddleware
{
 private readonly RequestDelegate _next;
 private readonly IFileProvider _fileProvider;
 public HelloFileProviderMiddleware(RequestDelegate next, IFileProvider fileProvider)
 {
  _next = next;
  _fileProvider = fileProvider;
 }
 public async Task Invoke(HttpContext context)
 {
  var output = new StringBuilder("");
  ResolveDirectory(output, "", "");
  await context.Response.WriteAsync(output.ToString());
 }
 private void ResolveDirectory(StringBuilder output, string path, string prefix)
 {
  IDirectoryContents dir = _fileProvider.GetDirectoryContents(path);
  foreach (IFileInfo item in dir)
  {
   if (item.IsDirectory)
   {
    output.AppendLine(prefix + "[" + item.Name + "]");
    ResolveDirectory(output,
     item.PhysicalPath.Substring(Directory.GetCurrentDirectory().Length),
     prefix + " ");
   } else
   {
    output.AppendLine(prefix + item.Name);
   }
  }
 }
} 

Run, the page effect:

====end by sanshi=========================

Source code download

summary

The above is a small series for you to introduce ASP.NET Core 2.0 local file operation problems and solutions, I hope to help you, if you have any questions, please leave me a message, the editor will reply to you in time. Thank you very much for your support to the developeppaer website!