如何根据特定日期获取文件? [英] How to get the files based on particular date?

查看:110
本文介绍了如何根据特定日期获取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Hi,

var files = (from c in rootDir.GetFiles()
                            where c.CreationTime >= DateTime.Now.Date.AddDays(-1)
                            select c).ToList();





以上代码工作正常它将返回昨天创建的文件以及今天但我只需要获取昨天创建的文件,如果我使用==而不是> =





the above code is working fine it will return yesterday created files as well as today's but i need to get only yesterday created files, for that if i use == instead of >=
i.e.

var files = (from c in rootDir.GetFiles()
                            where c.CreationTime == DateTime.Now.Date.AddDays(-1)
                            select c).ToList();





它不会返回任何文件



it will not returning any files

推荐答案

您正在尝试比较日期+时间值;显然,日期可能匹配,但不是时间。



以下是你可以做的事情:

You are trying to compare Date + Time values; obviously, dates may match, but not times.

Here's what you could do instead:
var files = (from c in rootDir.GetFiles()
             where (c.CreationTime >= DateTime.Today.AddDays(-1)) && (c.CreationTime < DateTime.Today)
             select c).ToList();





希望这会有所帮助。



Hope this helps.


var files =(from c in rootDir.GetFiles()

其中c.CreationTime> = DateTime.Now.Date.AddDays(-1)&c.CreationTime< DateTime.Now.Date

选择c).ToList();
var files = (from c in rootDir.GetFiles()
where c.CreationTime >= DateTime.Now.Date.AddDays(-1) & c.CreationTime < DateTime.Now.Date
select c).ToList();


var files = from c in directoryInfo.GetFiles()
            where c.CreationTime >somedate
            select c;


这篇关于如何根据特定日期获取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆