开发者

Sorting files from directoryinfo by date in asp.net

开发者 https://www.devze.com 2023-04-12 17:23 出处:网络
how can I sort (not filter) directoryinfo files by date (oldest to recent) ? I am using asp.net and 开发者_如何学运维visual studio 2008The same as @DaRKoN_ in vb.net:

how can I sort (not filter) directoryinfo files by date (oldest to recent) ? I am using asp.net and 开发者_如何学运维visual studio 2008


The same as @DaRKoN_ in vb.net:

Module Module1

    Sub Main()
        Dim orderedFiles = New System.IO.DirectoryInfo("c:\\").GetFiles().OrderBy(Function(x) x.CreationTime)
        For Each f As System.IO.FileInfo In orderedFiles
            Console.WriteLine(String.Format("{0,-15} {1,12}", f.Name, f.CreationTime.ToString))
        Next
    End Sub

End Module


The GetFiles() method on the DirectoryInfo class returns an Array, which implements IEnumerable. So you can apply all the standard LINQ extension methods.

var orderedFiles = new System.IO.DirectoryInfo("path")
                       .GetFiles()
                       .OrderBy(x => x.CreationTime);

Edit: Just realised this is tagged with VB. Also see the comment by Jon on the OP re: existing answers.


This was tagged vb (which is why I came across it.) I thought I would throw the vb answer up there.

    Dim sDir As String = HttpRuntime.AppDomainAppPath
    Dim oDirInfo As System.IO.DirectoryInfo
    Dim oInfo As System.IO.FileInfo

    oDirInfo = New System.IO.DirectoryInfo(sDir)

    oInfo = oDirInfo.GetFiles().OrderByDescending(Function(p) p.LastWriteTime).First()

    return oInfo.LastWriteTime
0

精彩评论

暂无评论...
验证码 换一张
取 消