0

I am writing a asp.net+c# code to get all files in a directory I am using the following statements:

 string[] files = Directory.GetFiles(Server.MapPath("~/someFolder"));

my question is when executing this statement, what is the behavior of GetFiles here? is there any criteria for storing the files in the array? I mean does executing this statement many times brings the files with the same order? does it get them ordered by date, name, ?

IQJ
  • 1
  • Related : http://stackoverflow.com/questions/52842/sorting-directory-getfiles –  Aug 07 '16 at 09:55

2 Answers2

0

You can ordered the files list by name, size and date. To sort by name,

var sorted = files.OrderBy(file => file);

To sort by size,

var sorted = files.OrderBy(file=> new FileInfo(file).Length);

To sort by date,

var sorted = files.OrderBy(file=> file.CreationTime);
Elvin Mammadov
  • 25,329
  • 11
  • 40
  • 82
0

User this code:

var lst = new DirectoryInfo(Server.MapPath("~/someFolder")).GetFiles()
              .OrderBy(x => new { x.CreationTime, x.Name });

Use linq to order your list, then

string[] files = lst.Select(x => x.FullName).ToArray();
MSL
  • 990
  • 1
  • 12
  • 28