欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.net core 讀取本地指定目錄下的文件的實(shí)例代碼

 更新時(shí)間:2018年09月16日 09:43:38   作者:蝸牛丨  
這篇文章主要介紹了.net core 讀取本地指定目錄下的文件的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下

項(xiàng)目需求

asp.net core 讀取log目錄下的.log文件,.log文件的內(nèi)容如下:

xxx.log

------------------------------------------begin---------------------------------
寫入時(shí)間:2018-09-11 17:01:48
 userid=1000
 golds=10
 -------------------------------------------end---------------------------------

一個(gè) begin end 為一組,同一個(gè).log文件里 userid 相同的,取寫入時(shí)間最大一組值,所需結(jié)果如下:

UserID   Golds   RecordDate
 1001     20     2018/9/11 17:10:48 
 1000     20     2018/9/11 17:11:48 
 1003     30     2018/9/11 17:12:48 
 1002     10     2018/9/11 18:01:48
 1001     20     2018/9/12 17:10:48 
 1000     30     2018/9/12 17:12:48 
 1002     10     2018/9/12 18:01:48

項(xiàng)目結(jié)構(gòu)

Snai.File.FileOperation  Asp.net core 2.0 網(wǎng)站

項(xiàng)目實(shí)現(xiàn)

新建Snai.File解決方案,在解決方案下新建一個(gè)名Snai.File.FileOperation Asp.net core 2.0 空網(wǎng)站

把log日志文件拷備到項(xiàng)目下

修改Startup類的ConfigureServices()方法,注冊(cè)訪問本地文件所需的服務(wù),到時(shí)在中間件中通過構(gòu)造函數(shù)注入添加到中間件,這樣就可以在一個(gè)地方控制文件的訪問路徑(也就是應(yīng)用程序啟動(dòng)的時(shí)候)

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

新建 Middleware 文件夾,在 Middleware下新建 Entity 文件夾,新建 UserGolds.cs 類,用來保存讀取的日志內(nèi)容,代碼如下

namespace Snai.File.FileOperation.Middleware.Entity
{
 public class UserGolds
 {
  public UserGolds()
  {
   RecordDate = new DateTime(1970, 01, 01);
   UserID = 0;
   Golds = 0;
  }
  public DateTime RecordDate { get; set; }
  public int UserID { get; set; }
  public int Golds { get; set; }
 }
}

 在 Middleware 下新建 FileProviderMiddleware.cs 中間件類,用于讀取 log 下所有日志文件內(nèi)容,并整理成所需的內(nèi)容格式,代碼如下

namespace Snai.File.FileOperation.Middleware
{
 public class FileProviderMiddleware
 {
  private readonly RequestDelegate _next;
  private readonly IFileProvider _fileProvider;
  public FileProviderMiddleware(RequestDelegate next, IFileProvider fileProvider)
  {
   _next = next;
   _fileProvider = fileProvider;
  }
  public async Task Invoke(HttpContext context)
  {
   var output = new StringBuilder("");
   //ResolveDirectory(output, "", "");
   ResolveFileInfo(output, "log", ".log");
   await context.Response.WriteAsync(output.ToString());
  }
  //讀取目錄下所有文件內(nèi)容
  private void ResolveFileInfo(StringBuilder output, string path, string suffix)
  {
   output.AppendLine("UserID Golds RecordDate");
   IDirectoryContents dir = _fileProvider.GetDirectoryContents(path);
   foreach (IFileInfo item in dir)
   {
    if (item.IsDirectory)
    {
     ResolveFileInfo(output,
      item.PhysicalPath.Substring(Directory.GetCurrentDirectory().Length),
      suffix);
    }
    else
    {
     if (item.Name.Contains(suffix))
     {
      var userList = new List<UserGolds>();
      var user = new UserGolds();
      IFileInfo file = _fileProvider.GetFileInfo(path + "\\" + item.Name);
      using (var stream = file.CreateReadStream())
      {
       using (var reader = new StreamReader(stream))
       {
        string content = reader.ReadLine();
        while (content != null)
        {
         if (content.Contains("begin"))
         {
          user = new UserGolds();
         }
         if (content.Contains("寫入時(shí)間"))
         {
          DateTime recordDate;
          string strRecordDate = content.Substring(content.IndexOf(":") + 1).Trim();
          if (DateTime.TryParse(strRecordDate, out recordDate))
          {
           user.RecordDate = recordDate;
          }
         }
         if (content.Contains("userid"))
         {
          int userID;
          string strUserID = content.Substring(content.LastIndexOf("=") + 1).Trim();
          if (int.TryParse(strUserID, out userID))
          {
           user.UserID = userID;
          }
         }
         if (content.Contains("golds"))
         {
          int golds;
          string strGolds = content.Substring(content.LastIndexOf("=") + 1).Trim();
          if (int.TryParse(strGolds, out golds))
          {
           user.Golds = golds;
          }
         }
         if (content.Contains("end"))
         {
          var userMax = userList.FirstOrDefault(u => u.UserID == user.UserID);
          if (userMax == null || userMax.UserID <= 0)
          {
           userList.Add(user);
          }
          else if (userMax.RecordDate < user.RecordDate)
          {
           userList.Remove(userMax);
           userList.Add(user);
          }
         }
         content = reader.ReadLine();
        }
       }
      }
      if (userList != null && userList.Count > 0)
      {
       foreach (var golds in userList.OrderBy(u => u.RecordDate))
       {
        output.AppendLine(golds.UserID.ToString() + " " + golds.Golds + " " + golds.RecordDate);
       }
       output.AppendLine("");
      }
     }
    }
   }
  }
  //讀取目錄下所有文件名
  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(path + prefix + item.Name);
    }
   }
  }
 }
 public static class UseFileProviderExtensions
 {
  public static IApplicationBuilder UseFileProvider(this IApplicationBuilder app)
  {
   return app.UseMiddleware<FileProviderMiddleware>();
  }
 }
}

上面有兩個(gè)方法 ResolveFileInfo()和ResolveDirectory()

ResolveFileInfo()  讀取目錄下所有文件內(nèi)容,也就是需求所用的方法

ResolveDirectory() 讀取目錄下所有文件名,是輸出目錄下所有目錄和文件名,不是需求所需但也可以用

修改Startup類的Configure()方法,在app管道中使用文件中間件服務(wù)

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
  if (env.IsDevelopment())
  {
    app.UseDeveloperExceptionPage();
  }

  app.UseFileProvider();
  
  app.Run(async (context) =>
  {
    await context.Response.WriteAsync("Hello World!");
  });
}

到此所有代碼都已編寫完成

啟動(dòng)運(yùn)行項(xiàng)目,得到所需結(jié)果,頁(yè)面結(jié)果如下

源碼訪問地址:https://github.com/Liu-Alan/Snai.File

總結(jié)

以上所述是小編給大家介紹的.net core 讀取本地指定目錄下的文件的相關(guān)知識(shí),希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

  • .Net Core路由處理的知識(shí)點(diǎn)與方法總結(jié)

    .Net Core路由處理的知識(shí)點(diǎn)與方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于.Net Core路由處理的知識(shí)點(diǎn)與方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • ASP.NET實(shí)現(xiàn)多域名多網(wǎng)站共享Session值的方法

    ASP.NET實(shí)現(xiàn)多域名多網(wǎng)站共享Session值的方法

    實(shí)現(xiàn)功能:可設(shè)置哪些站點(diǎn)可以共享Session值,這樣就防止別人利用這個(gè)去訪問,要想實(shí)現(xiàn)這個(gè)功能就必須得把Session值 放入數(shù)據(jù)庫(kù)中, 所有我們先在VS命令工具下注冊(cè)一個(gè)
    2011-11-11
  • 一個(gè)Asp.Net的顯示分頁(yè)方法 附加實(shí)體轉(zhuǎn)換和存儲(chǔ)過程 帶源碼下載

    一個(gè)Asp.Net的顯示分頁(yè)方法 附加實(shí)體轉(zhuǎn)換和存儲(chǔ)過程 帶源碼下載

    現(xiàn)在自己寫的webform都不用服務(wù)器控件了,所以自己仿照aspnetpager寫了一個(gè)精簡(jiǎn)實(shí)用的返回分頁(yè)顯示的html方法,其他話不說了,直接上代碼
    2012-10-10
  • CommunityServer又稱CS論壇的相關(guān)學(xué)習(xí)資料

    CommunityServer又稱CS論壇的相關(guān)學(xué)習(xí)資料

    以前項(xiàng)目需要整合這個(gè)論壇,同事找了一些資料,現(xiàn)在放上來,并說下自己對(duì)這個(gè)論壇的看法。
    2009-05-05
  • ASP.NET中驗(yàn)證控件的使用方法

    ASP.NET中驗(yàn)證控件的使用方法

    這篇文章主要內(nèi)容是ASP.NET中驗(yàn)證控件的使用方法,RequiredFieldValidation控件的介紹,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2015-08-08
  • ASP.net的驗(yàn)證控件淺析

    ASP.net的驗(yàn)證控件淺析

    前些天在做注冊(cè)頁(yè)面的驗(yàn)證的時(shí)候,用了下ASP.net的驗(yàn)證控件,有一些體會(huì),特寫下這篇博客,如果有朋友有不同ideas,歡迎大家留言
    2011-11-11
  • asp.net Core3.0區(qū)域與路由配置的方法

    asp.net Core3.0區(qū)域與路由配置的方法

    這篇文章主要給大家介紹了關(guān)于asp.net Core3.0區(qū)域與路由配置的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用asp.net Core3.0具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探

    ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探

    這篇文章主要介紹了ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探,幫助大家更好的理解和學(xué)習(xí)使用.NET技術(shù),感興趣的朋友可以了解下
    2021-03-03
  • Asp.net mvc實(shí)時(shí)生成縮率圖到硬盤

    Asp.net mvc實(shí)時(shí)生成縮率圖到硬盤

    這篇文章主要介紹了Asp.net mvc實(shí)時(shí)生成縮率圖到硬盤的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • .NET關(guān)于API 句柄泄漏分析

    .NET關(guān)于API 句柄泄漏分析

    本文主要介紹了.NET關(guān)于API 句柄泄漏分析,文中結(jié)合代碼與圖片講解的非常詳細(xì),感興趣的小伙伴可以自行參考一下
    2021-08-08

最新評(píng)論