Sort files on Date modified or created in asp.net using C# -
i need sort files based on date , time last created or modified , show latest added file in first. asp.net form uploading files on webserver , after uploading, default files organised on basis of name or should in alphabetic order. so, can 1 me sort , organise on basis of time uploaded.
protected void getfiles() { system.text.stringbuilder sbld = new system.text.stringbuilder(); if (directory.exists(server.mappath("~/package_image/"))) { directoryinfo dirmail = new directoryinfo(server.mappath("~/package_image/")); fileinfo[] defaultfiles = dirmail.getfiles(); foreach (fileinfo filedir in defaultfiles) { if (filedir.extension.tolower() == ".jpg" || filedir.extension.tolower() == ".gif" || filedir.extension.tolower() == ".png" || filedir.extension.tolower() == ".jpeg" || filedir.extension.tolower() == ".bmp") { // need sorting on basis of date-time, created or uploaded. sbld.append("<div class='itembox'><table width='100%'><tr><td height='160'><img width='200' src='../package_image/" + filedir.name + "'></img></td></tr></table></div>"); } } literal1.text = (sbld.tostring()); } }
the sorting might using c#, done server itself, or if possible can javascript or jquery, can processed @ client itself.
quite simple. make sure have reference system.linq added next other using statements.
//using system.linq protected void getfiles() { system.text.stringbuilder sbld = new system.text.stringbuilder(); if (directory.exists(server.mappath("~/package_image/"))) { directoryinfo dirmail = new directoryinfo(server.mappath("~/package_image/")); fileinfo[] orig = dirmail.getfiles(); // sort on server fileinfo[] defaultfiles = (from file in orig orderby file.creationtime select file).toarray(); foreach (fileinfo filedir in defaultfiles) { if (filedir.extension.tolower() == ".jpg" || filedir.extension.tolower() == ".gif" || filedir.extension.tolower() == ".png" || filedir.extension.tolower() == ".jpeg" || filedir.extension.tolower() == ".bmp") { // need sorting on basis of date-time, created or uploaded. sbld.append("<div class='itembox'><table width='100%'><tr><td height='160'><img width='200' src='../package_image/" + filedir.name + "'></img></td></tr></table></div>"); } } literal1.text = (sbld.tostring()); } }
Comments
Post a Comment