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 ?
Asked
Active
Viewed 4.9k times
2 Answers
60
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);
-
4Also love this answer [here](http://stackoverflow.com/a/13867061/4009972) by @nicholas-carey that suggests `Directory.EnumerateFiles()` over `Directory.GetFiles()` – codeMonkey Apr 07 '17 at 17:00
-
Another downvote, seriously? Please downvoters, leave a note explaining the reason my post is bad so that I can eventually amend it and give the community a better answer! – Marco May 23 '18 at 10:03
-
1I suspect the use of "GetFiles". This is first gets a complete list of all files and then passes the complete array onto the where clause. On a file system with a large number of files this would be inefficient. Enumeratefiles passes each file onto the where clause. – Adam Green Aug 05 '18 at 16:52
1
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 :)

Community
- 1
- 1

Davide Piras
- 43,984
- 10
- 98
- 147