Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting files by creation date in .NET

Tags:

c#

.net

file

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.

like image 579
Senseful Avatar asked Jan 22 '11 02:01

Senseful


People also ask

How can I tell when a file was created in C#?

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.

How do I find the creation date of a file?

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.

How do I find the original Windows file creation date?

Right click - select Properties. Click the Details tab. Look for the Origin section.

How do I get the last modified date of a file in VB NET?

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.


2 Answers

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... } 
like image 91
George Taskos Avatar answered Sep 27 '22 21:09

George Taskos


You can use Linq

var files = Directory.GetFiles(@"C:\", "*").OrderByDescending(d => new FileInfo(d).CreationTime); 
like image 24
Sev Avatar answered Sep 27 '22 19:09

Sev