Get a list of files in a directory in descending order by creation date using C#

Viewed 49039

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).

3 Answers
Related