使用C#在asp.net中修改或创建的日期对文件进行排序

时间:2013-04-05 03:59:52

标签: c# asp.net file-upload

我需要根据上次创建或修改的日期和时间对文件进行排序,并在第一个文件中显示最新添加的文件。基本上,asp.net表单用于上传网络服务器上的文件,上传后,默认情况下文件根据其名称进行组织,或者我应按字母顺序说明。所以,任何人都可以帮助我根据上传时间对其进行排序和组织。

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());
    }
}

排序可能是使用C#,这将由服务器本身完成,或者如果可能,我可以使用javascript或jquery,以便可以在客户端本身处理。

2 个答案:

答案 0 :(得分:2)

非常简单。确保您在其他using语句旁边添加了对System.Linq的引用。

//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());
    }
}

答案 1 :(得分:0)

'System.Linq'可以提供帮助。包含一个using语句。

然后你可以写一个lambda表达式或像这样的linq查询

Lambda Expression:

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

Linq查询:

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

如果您需要先按日期时间降序排序&amp;然后按名称升序排序,

Lambda Expression:

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

Linq查询:

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