Getting files by creation date in .NET

C#.NetFile

C# Problem Overview


I have a folder which contains many files. Is there any easy way to get the file names in the directory sorted by their creation date/time?

If I use Directory.GetFiles(), it returns the files sorted by their file name.

C# Solutions


Solution 1 - C#

this could work for you.

using System.Linq;

DirectoryInfo info = new DirectoryInfo("PATH_TO_DIRECTORY_HERE");
FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
foreach (FileInfo file in files)
{
    // DO Something...
}

Solution 2 - C#

You can use Linq

var files = Directory.GetFiles(@"C:\", "*").OrderByDescending(d => new FileInfo(d).CreationTime);

Solution 3 - C#

If you don't want to use LINQ

// Get the files
DirectoryInfo info = new DirectoryInfo("path/to/files"));
FileInfo[] files = info.GetFiles();

// Sort by creation-time descending 
Array.Sort(files, delegate(FileInfo f1, FileInfo f2)
{
    return f2.CreationTime.CompareTo(f1.CreationTime);
});

Solution 4 - C#

This returns the last modified date and its age.

DateTime.Now.Subtract(System.IO.File.GetLastWriteTime(FilePathwithName).Date)

Solution 5 - C#

@jing: "The DirectoryInfo solution is much faster then this (especially for network path)"

I cant confirm this. It seems as if Directory.GetFiles triggers a filesystem or network cache. The first request takes a while, but the following requests are much faster, even if new files were added. In my test I did a Directory.getfiles and a info.GetFiles with the same patterns and both run equally

GetFiles  done 437834 in00:00:20.4812480
process files  done 437834 in00:00:00.9300573
GetFiles by Dirinfo(2)  done 437834 in00:00:20.7412646

Solution 6 - C#

If the performance is an issue, you can use this command in MS_DOS:

dir /OD >d:\dir.txt

This command generate a dir.txt file in *d:* root the have all files sorted by date. And then read the file from your code. Also, you add other filters by * and ?.

Solution 7 - C#

            DirectoryInfo dirinfo = new DirectoryInfo(strMainPath);
            String[] exts = new string[] { "*.jpeg", "*.jpg", "*.gif", "*.tiff", "*.bmp","*.png", "*.JPEG", "*.JPG", "*.GIF", "*.TIFF", "*.BMP","*.PNG" };
            ArrayList files = new ArrayList();
            foreach (string ext in exts)
                files.AddRange(dirinfo.GetFiles(ext).OrderBy(x => x.CreationTime).ToArray());

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSensefulView Question on Stackoverflow
Solution 1 - C#George TaskosView Answer on Stackoverflow
Solution 2 - C#SevView Answer on Stackoverflow
Solution 3 - C#HenrikView Answer on Stackoverflow
Solution 4 - C#BMGView Answer on Stackoverflow
Solution 5 - C#PasserView Answer on Stackoverflow
Solution 6 - C#grandhougdayView Answer on Stackoverflow
Solution 7 - C#Ata HoseiniView Answer on Stackoverflow