Getting all files modified within a date range

user339160 picture user339160 · Aug 31, 2011 · Viewed 44.9k times · Source

We have a asp.net,C# application in which there is a requirement to get all the files whose date modified will be b/w startdate and enddate . How can we achieve this ? Also want to get all the files not modified for last 3 months ?

Answer

Marco picture Marco · Aug 31, 2011

According to this post, you could do this:

var directory = new DirectoryInfo(your_dir);
DateTime from_date = DateTime.Now.AddMonths(-3);
DateTime to_date = DateTime.Now;
var files = directory.GetFiles()
  .Where(file=>file.LastWriteTime >= from_date && file.LastWriteTime <= to_date);