.net core 读取本地指定目录下的文件的实例代码

所属分类: 网络编程 / ASP.NET 阅读数: 1867
收藏 0 赞 0 分享

项目需求

asp.net core 读取log目录下的.log文件,.log文件的内容如下:

xxx.log

------------------------------------------begin---------------------------------
写入时间:2018-09-11 17:01:48
 userid=1000
 golds=10
 -------------------------------------------end---------------------------------

一个 begin end 为一组,同一个.log文件里 userid 相同的,取写入时间最大一组值,所需结果如下:

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

项目结构

Snai.File.FileOperation  Asp.net core 2.0 网站

项目实现

新建Snai.File解决方案,在解决方案下新建一个名Snai.File.FileOperation Asp.net core 2.0 空网站

把log日志文件拷备到项目下

修改Startup类的ConfigureServices()方法,注册访问本地文件所需的服务,到时在中间件中通过构造函数注入添加到中间件,这样就可以在一个地方控制文件的访问路径(也就是应用程序启动的时候)

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

新建 Middleware 文件夹,在 Middleware下新建 Entity 文件夹,新建 UserGolds.cs 类,用来保存读取的日志内容,代码如下

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 下所有日志文件内容,并整理成所需的内容格式,代码如下

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());
  }
  //读取目录下所有文件内容
  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("写入时间"))
         {
          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>();
  }
 }
}

上面有两个方法 ResolveFileInfo()和ResolveDirectory()

ResolveFileInfo()  读取目录下所有文件内容,也就是需求所用的方法

ResolveDirectory() 读取目录下所有文件名,是输出目录下所有目录和文件名,不是需求所需但也可以用

修改Startup类的Configure()方法,在app管道中使用文件中间件服务

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

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

到此所有代码都已编写完成

启动运行项目,得到所需结果,页面结果如下

源码访问地址:https://github.com/Liu-Alan/Snai.File

以上所述是小编给大家介绍的.net core 读取本地指定目录下的文件的相关知识,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

更多精彩内容其他人还在看

.NET Core源码解析配置文件及依赖注入

这篇文章我们设计了一些复杂的概念,因为要对ASP.NET Core的启动及运行原理、配置文件的加载过程进行分析,依赖注入,控制反转等概念的讲解等
收藏 0 赞 0 分享

.NET Corek中Git的常用命令及实战演练

这篇文章将通过故事的形式从Git的历史谈起,并讲述Git的强大之处。然后通过实战演练教你如何在Github以及码云上托管我们的代码并进行代码的版本控制
收藏 0 赞 0 分享

Asp.Net Core WebAPI使用Swagger时API隐藏和分组详解

这篇文章主要给大家介绍了关于Asp.Net Core WebAPI使用Swagger时API隐藏和分组的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Asp.Net Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

如何利用FluentMigrator实现数据库迁移

这篇文章主要给大家介绍了关于如何利用FluentMigrator实现数据库迁移的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

ASP.NET Core利用Jaeger实现分布式追踪详解

这篇文章主要给大家介绍了关于ASP.NET Core利用Jaeger实现分布式追踪的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

浅谈从ASP.NET Core2.2到3.0你可能会遇到这些问题

这篇文章主要介绍了ASP.NET Core2.2到3.0可能会遇到的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解.net core webapi 前后端开发分离后的配置和部署

这篇文章主要介绍了.net core webapi 前后端开发分离后的配置和部署,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁

这篇文章主要介绍了ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

.net 4.5部署到docker容器的完整步骤

这篇文章主要给大家介绍了关于.net 4.5部署到docker容器的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用.net4.5具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.net core并发下线程安全问题详解

这篇文章主要给大家介绍了关于.net core并发下线程安全问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多