2

I need to sort files based on the date and time they were last created or modified and show the latest added file in the first. Basically the asp.net form is for uploading the files on the webserver and after uploading, by default the files get organised on the basis of the its name or I should say in alphabetic order. So, can any one help me sort and organise it on the basis of the time it was 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 the basis of date-time, it was 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 be using C#, that will be done by the server itself, or if possible can I do it with javascript or jquery, so that it can be processed at the client itself.

2 Answers 2

3

Quite simple. Make sure you have a reference to System.Linq added next to 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 the basis of date-time, it was 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());
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the quick help, but I am not at all aware of LINQ queries, and I am getting an error A query body must end with a select clause or a group clause where you have written the query.
@Cyberpks sorry for that, wrote it off the top of my head. Just fixed the code by actually using a compiler. Try it now
Now that's perfect :D, really appreciate your help. By the way I required it to generate in Descending order so I tweaked the query a little from file in orig orderby file.CreationTime descending select file. Thanks for your help, friend.
0

'System.Linq' can help. Include a using statement for it.

And then you can write a lambda expression OR a linq query like this

Lambda Expression :

FileInfo[] SortedFiles = dirMail.GetFiles().OrderBy(file => file.CreationTime).ToArray();

Linq Query :

FileInfo[] SortedFiles = (from file in dirMail.GetFiles() order by file.CreationTime select file).ToArray();

If you need to first sort by date time descending & then sort by name ascending,

Lambda Expression :

FileInfo[] SortedFiles = dirMail.GetFiles().OrderByDescending(file => file.CreationTime).ThenBy(file => file.Name).ToArray();

Linq Query :

FileInfo[] SortedFiles = (from file in dirMail.GetFiles() orderby file.CreationTime descending, file.Name ascending select file).ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.