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.
The CreationTime property returns the DateTime when a file was created. Code snippt to check check when a file was created in C#. The CreationTime property returns the DateTime when a file was created.
You can use this code to see the last modified date of a file. DateTime dt = File. GetLastWriteTime(path); And this code to see the creation time.
Right click - select Properties. Click the Details tab. Look for the Origin section.
From File. GetLastWriteTime Method: If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.
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... }
You can use Linq
var files = Directory.GetFiles(@"C:\", "*").OrderByDescending(d => new FileInfo(d).CreationTime);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With