-2

I'm looking for a way to specifically GetFiles that have a specific name AND that have been created within a certain time limit, however I can't figure out how to use both criteria in the same statement, or to find another method to accomplish this.

Here's my code so far:

DirectoryInfo dir = new DirectoryInfo(folderPath);

FileInfo[] files = dir.GetFiles("ArchiveDeleteLog*" , SearchOption.TopDirectoryOnly);

1 Answers1

0

You can add a Where clause to your GetFiles call to filter on files that were created within a specific timeframe:

// Adjust these to the date range you are interested in
DateTime startDate = DateTime.MinValue;
DateTime endDate = DateTime.MaxValue;

List<FileInfo> files = dir.GetFiles("ArchiveDeleteLog*", SearchOption.TopDirectoryOnly)
    .Where(fileInfo =>
        fileInfo.CreationTime >= startDate &&
        fileInfo.CreationTime <= endDate)
    .ToList();
Rufus L
  • 36,127
  • 5
  • 30
  • 43