Click here to Skip to main content
15,847,263 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code that lists all the subdirectories in a folder.
string path = @"C:\Temp\MyFolder";
DirectoryInfo dir_place = new DirectoryInfo(path);
//place directories in an array
DirectoryInfo[] directories = dir_place.GetDirectories();

//add them to ListBox
foreach (var directory in directories)

I am trying to list all directories based on specified date range

What I have tried:

Only the above code as I am searching for help
Updated 7-Apr-23 4:44am

Something like this?
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

// directories in approx the last 6 months
DateTime compareDate = DateTime.Now + TimeSpan.FromDays(-6 * 30.5);
IEnumerable<DirectoryInfo> searchResults = new DirectoryInfo(path)
    .Where(info => info.CreationTime <= compareDate)
    .OrderByDescending(di => di.CreationTime);

foreach (DirectoryInfo info in searchResults)
    Console.WriteLine($"{info.Name} | {info.CreationTime} | {info.FullName}");
Share this answer
George Swan 7-Apr-23 13:01pm    
Should not that be DateTime compareDate = DateTime.Now - TimeSpan.FromDays(6 * 30.5);?
Graeme_Grant 7-Apr-23 17:56pm    
Good to see someone is paying attention.
Richard Deeming 11-Apr-23 6:33am    
Or better yet:
DateTime compareDate = DateTime.Today.AddMonths(-6);

It may also be better to use UTC; there's a good chance that a six month period would include at least one clock change for DST.
Graeme_Grant 11-Apr-23 6:39am    
That works too ... what I wrote was a quick post, the core o how is there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900