I am using Directory.GetFiles to get files from a particular folder. By default files from that folder are coming sort by filename ie. in alphabetical order of filename. I want to get files in the order in which files are modified.
I cannot use Directory.GetInfo as I want to get the files which contain particular keyword. Please let me know how can we get the file sorted by their modified date. I am using the following code
string[] arr1 = Directory.GetFiles("D:/TestFolder", "*"Test12"*");
Any help would be greatly appreciated.
what about the below
DirectoryInfo di = new DirectoryInfo("D:\\TestFolder");
FileSystemInfo[] files = di.GetFileSystemInfos();
var orderedFiles = files.Where(f=>f.Name.StartsWith("Test12"))
.OrderBy(f => f.CreationTime)
.ToList();
you can replace f.Name.StartWith
with any string function against your need (.Contains
,etc)
you can replace f => f.CreationTime
with f.LastWriteTime
to get the modified time but bear in mind that starting in Windows Vista, last access time is not updated by default. This is to improve file system performance
if you change to directory info you could do
FileInfo[] files = new DirectoryInfo("path")
.GetFiles("filter")
.OrderBy(f => f.CreationTime)
.ToArray();
Edit:
Saw you wanted modified date, can do that with f.LastWriteTime instead
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