tags:

views:

37

answers:

2

Hello everyone,

how can I read the files from a directory in the same order as displayed in the windows explorer?

For example, the windows explorer lists:

file 1.txt
file 2.txt
file 3.txt
file 4.txt
file 5.txt
file 6.txt
file 7.txt
file 8.txt
file 9.txt
file 10.txt
file 11.txt
file 12.txt

while Directory.GetFiles() lists:

file 1.txt
file 10.txt
file 11.txt
file 12.txt
file 2.txt
file 3.txt
file 4.txt
file 5.txt
file 6.txt
file 7.txt
file 8.txt
file 9.txt

[EDIT]

Here you can find another solution for this: http://www.codeproject.com/KB/recipes/csnsort.aspx?msg=3342284

A: 

That special ordering was a new feature added in WinXP. Your best bet would be to re-implement the same sorting algorithm (see the link above for how to do it) on the collection once you've loaded the collection using Directory.GetFiles().

Neil Barnwell
+3  A: 

What you are looking for is a natural sort. You can implement the one built in to Windows by following the accepted answer here: http://stackoverflow.com/questions/248603/natural-sort-order-in-c

RedFilter
Accepted answer for bringing me on the right track by mentioning "natural sort".
Inno