Get a list of files in a directory in descending order by creation date using C#
c#, file
Solution
You can use OrderByDescending
DirectoryInfo dir = new DirectoryInfo (folderpath);
FileInfo[] files = dir.GetFiles().OrderByDescending(p => p.CreationTime).ToArray();
Problem
I want to get a list of files in a folder sorted by their creation date using C#. I am using the following code: ``` if(Directory.Exists(folderpath)) { DirectoryInfo dir=new DirectoryInfo (folderpath); FileInfo[] files = dir.GetFiles().OrderBy(p=>p.CreationTime).ToArray(); foreach (FileInfo file in files) { ...... } } ``` This will give the ascending order of the creation time. I actually want get the most recently created file in the first position of my array (descending order).