black mamba
black mamba

Reputation: 25

How can I get the file creation date in a folder?

I want to get the date creation of all the files in the folder and sort them, but if I use the following code then for all the files, I get the date creation of the folder:

DateTime fileCreatedDate = File.GetCreationTime(@"d:\images");
var files = Directory.GetFiles(@"d:\images");
foreach (var file in files)
Console.WriteLine(file + " " + fileCreatedDate); 

Upvotes: 0

Views: 1191

Answers (1)

Jesse
Jesse

Reputation: 1424

That's because you're only getting the creation date of the folder. If you're just looking for the file creation date and not the folder, I would suggest moving File.GetCreationTime() into your for loop like so:

var files = Directory.GetFiles(@"d:\images");
foreach (var file in files){
    DateTime fileCreatedDate = File.GetCreationTime(Path.Combine(@"d:\images\", file)); // Append the file name to the end of your path
    Console.WriteLine(file + " " + fileCreatedDate);

    // Additional logic
}

Upvotes: 2

Related Questions