Accepted answer

simply change your for loop :

foreach (fileinfo f in directory.getfiles().orderby(fi=>fi.filename))


alternatively, you can rewrite the whole loop using this code :

var sortedfiles = from fi in directory.getfiles()
                  order by fi.filename
                  select new picture { imagepath = path, creationdate = f.creationtime, filename = f.filename };



note that enumeratefiles performs lazy loading and can be more efficient for larger directories, so:

dir.enumeratefiles().orderby(f => f.filename))


you can use linq from the beginning:

var files = from f in directory.enumeratefiles()
    let pic = new picture(){
        imagepath = path;
        creationdate = f.creationtime;
        filename =;
    orderby pic.filename
    select pic;

note that directory.enumeratefiles(path) will be more efficient if only the filename is used.


listpictures = listpictures.orderby(x => x.filename).tolist();


you can use lambda expression and/or extension methods. for example:

listpictures.orderby(p => p.filename).tolist();

Related Query

More Query from same tag