themis themis - 22 days ago 11
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));
dirList.Add(rootDir);

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

foreach (DirectoryInfo dir in dirList)
{
fileList.AddRange(
dir.GetFiles(fileSearchPattern, SearchOption.TopDirectoryOnly));
}

return fileList;
}

Answer

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

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