4

how can I sort (not filter) directoryinfo files by date (oldest to recent) ? I am using asp.net and visual studio 2008

Frank
  • 2,015
  • 10
  • 36
  • 57
  • Looks like this question was handled in this posting [Sorting Directory.GetFiles()][1] [1]: http://stackoverflow.com/questions/52842/sorting-directory-getfiles – Jon Reopelle Oct 12 '11 at 12:13

3 Answers3

8

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
ChadT
  • 7,598
  • 2
  • 40
  • 57
apros
  • 2,848
  • 3
  • 27
  • 31
4

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.

ChadT
  • 7,598
  • 2
  • 40
  • 57
0

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