themis themis - 4 months ago 36
C# Question

Search files based on date created in c#

I have this function that I use to read a directory and get files with a specific search pattern. Is there a way to use a search pattern based on the created date or modified date?

public static List<FileInfo> GetFileList(string fileSearchPattern, string rootFolderPath)
DirectoryInfo rootDir = new DirectoryInfo(rootFolderPath);

List<DirectoryInfo> dirList = new List<DirectoryInfo>(
rootDir.GetDirectories("*", SearchOption.AllDirectories));

List<FileInfo> fileList = new List<FileInfo>();

foreach (DirectoryInfo dir in dirList)
dir.GetFiles(fileSearchPattern, SearchOption.TopDirectoryOnly));

return fileList;


No, but you could filter them quickly with Linq; something like:

var files = from c in directoryInfo.GetFiles() 
            where c.CreationTime >somedate
            select c;