Sort files from Directory

Hi All,

I have a requirement to search for specific files from a designated folder and i have got success in getting those filtered files based on the search criteria i am passing inside my code.

But i need to sort the filtered files based on their creation date as i want to fetch the oldest created file from the filtered files.

Since there can be around 100K records present in the directory, i am facing performance issue in fetching the single file. Below is the code i have written to fetch the filtered files.


System.String[] filePaths;

filePaths = System.IO.Directory::GetFiles(inputDirectory,fileToFilter);

// Here inputdirectory variable is the target folder and fileToFilter is something like ABC.

Even the count of filtered items can be more than 100 or 200 so need to sort the files with created date time.

Can any one help me in adding the sort operation by Creation date/Time ?

GetFiles() gives you only file names, therefore you’re missing the information you need for sorting: the creation datetime.

You can get that by using DirectoryInfo.GetFiles() or DirectoryInfo.EnumerateFiles().

I would probably create a simple C# class library (added to AOT, which you can do in AX 2012) with code like this:

DirectoryInfo dirInfo = new DirectoryInfo(inputDirectory, fileToFilter);
dirInfo.OrderBy(f => f.CreationTime).Select(f => f.Name);