Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

People also ask

What is the command to list all files with date and time of creating the file?

ls command ls – Listing contents of directory, this utility can list the files and directories and can even list all the status information about them including: date and time of modification or access, permissions, size, owner, group etc.

How do I sort files in descending order?

Sort a File Numerically By default, sort will arrange the data in ascending order. If you want to sort in descending order, reverse the arrangement using the -r option along with the -n flag in the command.

How do I sort files by date?

Click the sort option in the top right of the Files area and select Date from the dropdown. Once you have Date selected, you will see an option to switch between descending and ascending order.


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