Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort DirectoryInfo.GetFiles() [duplicate]

I am creating the image of the PowerPoint file programmatically. And after saving the Images to the local drive I am getting the files using DirectoryInfo.GetFiles().
I am saving the image files with the sequence numbers.

My Files: enter image description here

My issue is when I get the files, are not in the sequence I need them in. The files sequence which I am getting in the FileInfo[] is :

enter image description here

Can any one help me to solve this issue?

like image 550
Rahul Gokani Avatar asked Mar 15 '14 05:03

Rahul Gokani


2 Answers

The function doesn't make any guarantees about order but you can achieve the desired result with a simple LINQ query;

   FileInfo[] sortedFiles = DirectoryInfo.GetFiles().OrderByDescending(x => x.Name).ToArray();
like image 169
evanmcdonnal Avatar answered Oct 29 '22 22:10

evanmcdonnal


Try this

foreach (FileInfo fi in directory.GetFiles().OrderBy(fi=>fi.FileName))
{

}
like image 41
MusicLovingIndianGirl Avatar answered Oct 29 '22 22:10

MusicLovingIndianGirl