5

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?

Rahul Gokani
  • 1,688
  • 5
  • 25
  • 43
  • http://stackoverflow.com/questions/6294275/sorting-the-result-of-directory-getfiles-in-c-sharp – Nagaraj S Mar 15 '14 at 05:20
  • 1
    http://stackoverflow.com/questions/52842/sorting-directory-getfiles – bansi Mar 15 '14 at 05:20
  • @bansi: I don't have found solution in that questions that's why I asked. Please check the question again. Thank you. – Rahul Gokani Mar 15 '14 at 06:23
  • 1
    I have voted for re-open. by the time check this. `const Int32 templateLength = 4;/*size of the filename template (till the numeric part starts)*/ foreach (FileInfo info in dInfo.GetFiles().OrderBy(fi => fi.Name.Substring(0, templateLength)).ThenBy(fi => Conversion.Val(fi.Name.Substring(templateLength)))) { Debug.Print(info.Name); }` – bansi Mar 15 '14 at 09:56
  • Guys, you've chosen incorrect duplicate question. It's [Natural Sort Order in C#](http://stackoverflow.com/questions/248603/natural-sort-order-in-c-sharp) OP is looking for. – Athari Mar 15 '14 at 10:18
  • @bansi Thank you for this replay. I am still trying on it. – Rahul Gokani Mar 15 '14 at 13:32
  • the best answer for me was here :https://social.msdn.microsoft.com/Forums/vstudio/en-US/e4ec8ae3-dc6e-4027-8fed-1be69b6a1462/sorting-a-list-with-alphanumeric-names?forum=csharpgeneral – Kamornik Cola Mar 25 '19 at 07:18

2 Answers2

5

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();
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
1

Try this

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

}
MusicLovingIndianGirl
  • 5,909
  • 9
  • 34
  • 65