列出降序目录

时间:2011-08-09 14:46:49

标签: asp.net vb.net list

我正在尝试按目录名称按降序列出目录。它们仍按正在加入顺序列出。有人可以帮我修复下面的代码,因为我似乎无法让它正常工作。我没有收到以下代码的错误,它按名称按加号顺序显示列表。

        Dim oDirInfo As New DirectoryInfo(Server.MapPath("Test/" & Session("Test")) & sSubDir)
        Dim oDirs As DirectoryInfo() = oDirInfo.GetDirectories()
        For Each oDir As DirectoryInfo In oDirs
            oDir.GetDirectories().OrderByDescending(Function(eachDir) eachDir.Name).FirstOrDefault()
            Try
                Response.Write((((("<TR><TD class=""tdDir""><A href=""" & lsScriptName & "?path=" & lsWebPath) + oDir.Name & "&title=" & lsTitle & "&link=" & lsLink & """>") + oDir.Name & "</A></TD><TD class=""tdDir"">Dir</TD><TD class=""tdDir"" align=""right"">") + oDir.LastWriteTime & "</TD><TD class=""tdDir"" align=""right"">") & " Folder</TD></TR>")
            Catch ex As Exception
                Response.Write(("<TR><TD class=""tdDir"">" + oDir.Name & " (Error Accessing Directory)</TD><TD class=""tdDir"">Dir</TD><TD class=""tdDir"" align=""right"">") + oDir.LastWriteTime & "</TD><TD class=""tdDir"" align=""right"">? files</TD></TR>")
            End Try
        Next

2 个答案:

答案 0 :(得分:0)

将此项添加到For Each:

上方
Array.Reverse(oDirs)

由于GetDirectories()以递增的alpha顺序返回目录,所以你只需这样做。

答案 1 :(得分:0)

呃,对象前缀。你知道不再鼓励那些吗?

Dim ParentDir As New DirectoryInfo(Server.MapPath("Test/" & Session("Test")) & sSubDir)
For Each oDir As DirectoryInfo In ParentDir.GetDirectories().OrderByDescending(Function(eachDir) eachDir.Name)
    Try
        Response.Write((((("<TR><TD class=""tdDir""><A href=""" & lsScriptName & "?path=" & lsWebPath) + oDir.Name & "&title=" & lsTitle & "&link=" & lsLink & """>") + oDir.Name & "</A></TD><TD class=""tdDir"">Dir</TD><TD class=""tdDir"" align=""right"">") + oDir.LastWriteTime & "</TD><TD class=""tdDir"" align=""right"">") & " Folder</TD></TR>")
    Catch ex As Exception
        Response.Write(("<TR><TD class=""tdDir"">" + oDir.Name & " (Error Accessing Directory)</TD><TD class=""tdDir"">Dir</TD><TD class=""tdDir"" align=""right"">") + oDir.LastWriteTime & "</TD><TD class=""tdDir"" align=""right"">? files</TD></TR>")
    End Try
Next