目录中的文件按fileName升序排序 - 对于每个文件

时间:2014-11-17 13:09:16

标签: vb.net .net-4.0

如何更新我的代码,以便按创建的日期/时间顺序处理所有文件?

Private Sub StartupFindExistingFiles(Path As String, SearchPattern As String)
    Dim fileEntries As String() = Directory.GetFiles(Path, SearchPattern)
    For Each fileName As String In fileEntries
        PrintJOBFile(fileName)
    Next
End Sub

1 个答案:

答案 0 :(得分:1)

您可以将LINQ与File.GetCreationTime

一起使用
Dim orderedByCreation = From file In Directory.EnumerateFiles(Path, SearchPattern)
                        Let createdAt = System.IO.File.GetCreationTime(file)
                        Order By createdAt Ascending
                        Select file

For Each filePath As String In orderedByCreation 
    PrintJOBFile(filePath)
Next

请注意,您也可以使用GetFiles代替EnumerateFiles。后者可以更有效率。