亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ASP.NET Core 2.0 本地文件操作問題及解決方案

 更新時(shí)間:2017年10月31日 11:04:23   作者:三生石上(FineUI控件)  
這篇文章主要介紹了ASP.NET Core 2.0 本地文件操作問題及解決方案,需要的朋友可以參考下

問題

如何在ASP.NET Core 2.0中受限地訪問本地目錄和文件信息?

答案

新建一個(gè)空項(xiàng)目,修改Startup類,添加訪問本地文件所需的服務(wù):

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

創(chuàng)建一個(gè)中間件,讀取根目錄下的所有文件,輸出文件名:

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>();
 }
}

運(yùn)行,此時(shí)頁(yè)面效果:

當(dāng)然,我們也可以通過IFileProvider接口讀取單個(gè)文件信息:

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());
   }
  }
 }
}

運(yùn)行,此時(shí)頁(yè)面效果:

討論

ASP.NET Core 2.0提供了繼承自接口IFileProvider的PhysicalFileProvider類型,用于受限地訪問本地文件系統(tǒng),它是對(duì)System.IO.File的一個(gè)封裝。

我們可以在Startup的Configure()方法內(nèi)將IFileProvider配置為一個(gè)服務(wù),然后通過構(gòu)造函數(shù)注入添加到中間件或者控制器中。這樣可以在一個(gè)地方控制文件的訪問路徑(也就是應(yīng)用程序啟動(dòng)的時(shí)候)。

IFileProvider有兩個(gè)重要的方法:

1. GetDirectoryContents:返回IDirectoryContents接口??捎糜诒闅v某個(gè)目錄中的全部文件或目錄。

2. GetFileInfo:返回IFileInfo接口。通過其CreateReadSteam方法可以讀取文件內(nèi)容。

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

下面通過遞歸來(lái)遍歷根目錄下全部文件和目錄,修改中間件代碼:

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);
   }
  }
 }
} 

運(yùn)行,此時(shí)頁(yè)面效果:

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

源代碼下載

總結(jié)

以上所述是小編給大家介紹的ASP.NET Core 2.0 本地文件操作問題及解決方案,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • ASP.NET中MVC傳遞數(shù)據(jù)的幾種形式總結(jié)

    ASP.NET中MVC傳遞數(shù)據(jù)的幾種形式總結(jié)

    這篇文章主要介紹了ASP.NET中MVC傳遞數(shù)據(jù)的幾種形式,以實(shí)例形式較為詳細(xì)的分析總結(jié)了MVC數(shù)據(jù)傳遞的相關(guān)技巧與注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • ASP.NET?Core6.0-wwwroot文件夾無(wú)法訪問解決方法

    ASP.NET?Core6.0-wwwroot文件夾無(wú)法訪問解決方法

    ASP.NET?Core項(xiàng)目中的wwwroot文件夾被視為Web根文件夾,本文主要介紹了ASP.NET?Core6.0-wwwroot文件夾無(wú)法訪問解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-09-09
  • ASP.NET 2.0中的頁(yè)面輸出緩存

    ASP.NET 2.0中的頁(yè)面輸出緩存

    ASP.NET 2.0中的頁(yè)面輸出緩存...
    2006-09-09
  • ASP.NET如何自定義項(xiàng)目模板詳解

    ASP.NET如何自定義項(xiàng)目模板詳解

    這篇文章主要給大家介紹了關(guān)于ASP.NET如何自定義項(xiàng)目模板的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • asp.net一些很酷很實(shí)用的.Net技巧

    asp.net一些很酷很實(shí)用的.Net技巧

    方便使用asp.net編程的朋友,都是一些非常有用的東西
    2008-08-08
  • ASP.NET MVC處理文件上傳的小例子

    ASP.NET MVC處理文件上傳的小例子

    這篇文章介紹了ASP.NET MVC處理文件上傳的小例子,有需要的朋友可以參考一下
    2013-10-10
  • .NET Web開發(fā)之.NET MVC框架介紹

    .NET Web開發(fā)之.NET MVC框架介紹

    MVC是一種架構(gòu)設(shè)計(jì)模式,該模式主要應(yīng)用于圖形化用戶界面(GUI)應(yīng)用程序。那么什么是MVC?MVC由三部分組成:Model(模型)、View(視圖)及Controller(控制器)
    2014-03-03
  • Asp.Net使用服務(wù)器控件Image/ImageButton顯示本地圖片的方法

    Asp.Net使用服務(wù)器控件Image/ImageButton顯示本地圖片的方法

    Image/ImageButton服務(wù)器控件顯示本地的圖片,實(shí)現(xiàn)思路是數(shù)據(jù)庫(kù)中存放了圖片的相對(duì)地址,讀取數(shù)據(jù)庫(kù)中的地址,用控件加載顯示圖片。具體實(shí)現(xiàn)步驟大家參考下本文
    2017-08-08
  • System.Timers.Timer定時(shí)執(zhí)行程序示例代碼

    System.Timers.Timer定時(shí)執(zhí)行程序示例代碼

    如果是某個(gè)邏輯功能的定時(shí),可以將code放到邏輯功能的類的靜態(tài)構(gòu)造函數(shù)中,在該邏輯類第一次執(zhí)行時(shí),靜態(tài)構(gòu)造函數(shù)會(huì)被調(diào)用,則定時(shí)自然啟動(dòng)
    2013-06-06
  • WPF關(guān)鍵幀動(dòng)畫介紹與實(shí)現(xiàn)

    WPF關(guān)鍵幀動(dòng)畫介紹與實(shí)現(xiàn)

    這篇文章介紹了WPF關(guān)鍵幀動(dòng)畫與實(shí)現(xiàn)方式,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01

最新評(píng)論