我想要只抓取今天更新或者創建的檔案(今天最舊的檔案),可是如果沒有今天的檔案,會直接錯誤。
我想要改成如果今天沒有,就改成昨天的,或者更舊的資料。
下面是我目前的程式碼,不知道如何修改
DateTime today = DateTime.Now.Date;
DirectoryInfo di = new DirectoryInfo(@"C:\Users\Desktop\ME");
var Wfile = (from f in di.GetFiles("W*.xlsx").
Where(f => f.LastWriteTime == today || f.CreationTime.Date == today)
orderby f.LastWriteTime descending select f).Last();
LAMBDA跟LINQ可以不要混用嗎?你的 f 分不清了,我修改如下,可以明確用.Date來比對是否為當天
DateTime today = DateTime.Now.Date;
DirectoryInfo di = new DirectoryInfo(@"C:\Users\DESKTOP\Me");
var Wfile = di.GetFiles("*.xlsx")
.Where(f => f.Name.ToLower().StartsWith("w") && (f.LastWriteTime.Date <= today.Date)
.OrderBy(o => o.LastWriteTime)
.FirstOrDefault();
if(Wfile!=null)
Console.WriteLine(Wfile);
f.LastWriteTime == today || f.CreationTime.Date == today
改成f.LastWriteTime <= today || f.CreationTime.Date <= today
試試看