.net core 读取本地指定目录下的文件的实例代码
项目需求
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 读取本地指定目录下的文件的相关知识,希望对大家有所帮助
上一篇: Python写入数据到MP3文件中的方法
下一篇: Python读取Excel的方法实例分析