简体   繁体   English

根据日期过滤文件夹文件

[英]Filtering folder files based on Date

DirectoryInfo info = new DirectoryInfo(Directory Name);
//... Other code 
var allLines = info.GetFiles("*.dat")
                          .Where(p => p.CreationTime.Date ==DateTime.Today.Date)
                           .SelectMany(p => File.ReadAllLines(p.FullName));

I am trying to fetch files which have been created today . 我正在尝试获取今天已创建的文件。 But with this code I am get all the .dat files present in the folder which are over 3 months old. 但是使用此代码,我可以找到文件夹中存在的所有超过3个月的所有.dat文件。 Any help!! 任何帮助!! Newbie in C# C#中的新手

Try this 尝试这个

What I understood from the linq you provided is you want the files of type .dat whose creation date is today and do write operation. 我从您提供的linq中了解到,您希望创建日期为今天的.dat类型的文件并执行写操作。

   var allLines = info.GetFiles("*.dat")
                      .Where(p => p.CreationTime.Date > DateTime.Today.Date.AddMinutes(-1))
                       .SelectMany(p => File.ReadAllLines(p.FullName));

Looks like your: 看起来像你的:

.SelectMany(p => File.ReadAllLines(p.FullName));

is wrong. 是错的。

Why you are not using just: 为什么不只使用:

info.GetFiles("*.dat").Where(p => p.CreationTime.Date == DateTime.Today.Date).Select(p => p.FullName);

And you will get list of files 你会得到文件列表

The code is working fine . 该代码工作正常。 Only thing was, I had to delete the previous compiled/debug file which did not involve date filtering . 唯一的事情是,我不得不删除以前的不涉及日期过滤的已编译/调试文件。 It was therefore taking the same even though clean solution & rebuild solution was done . 因此,即使完成了干净的解决方案和重建解决方案,也采用了相同的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM