user339160
user339160

Reputation:

Getting all files modified within a date range

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 ?

Upvotes: 25

Views: 50612

Answers (2)

Marco
Marco

Reputation: 57593

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);

Upvotes: 61

Davide Piras
Davide Piras

Reputation: 44605

look at this question and answer:

How to find the most recent file in a directory using .NET, and without looping?

you can start from there and add your where clause to the provided LINQ query in the answer :)

Upvotes: 1

Related Questions