按集合中的文件日期排序

时间:2009-11-10 18:34:18

标签: vb.net .net-3.5

我不认为System.Collections.ObjectModel具有任何排序或按功能排序。

我有一个文件列表,我想按文件日期排序。

Dim list AS System.Collections.ObjectModel.ReadOnlyCollection(Of String)
list = My.Computer.FileSystem.GetFiles("C:\SearchFolder" _ 
    , FileIO.SearchOption.SearchByTopLevelOnly _
    , "TheFileName*.txt")

Dim sTheLastFile AS String

sTheLastFile = list.Max()

这将返回最后一个文件,但是基于文件名而不是日期。我想我需要补充一下 .OrderBy(...只是无法得到那部分。

3 个答案:

答案 0 :(得分:1)

using System.IO;

public static void Main()
{
    DirectoryInfo di = new DirectoryInfo("c:\\temp\\");
    FileSystemInfo[] files = di.GetFileSystemInfos("*.mp3");
    printFiles(files);

    Array.Sort(files, CompareFileByDate);
    printFiles(files);

}

public static int CompareFileByDate(FileSystemInfo f1, FileSystemInfo f2)
{
    return DateTime.Compare(f1.LastWriteTime, f2.LastWriteTime);
}

public static void printFiles(FileSystemInfo[] files)
{
    foreach(FileSystemInfo file in files)
    {
        Console.WriteLine(file.Name);
    }
    Console.WriteLine("********************************");
}

看看这对你有帮助。

我使用了LastWriteTime属性。您可以选择适用于您的任何人(CreationTimeLastAccessTime)。

编辑:当然,这可以使用c#3.0& amp;支持lambda表达式。

EDIT2:

from file in new DirectoryInfo(@"c:\temp\").GetFileSystemInfos("*.mp3")
orderby file.LastWriteTime
select file

EDIT3:上述c#代码的vb.net版本

from file in new DirectoryInfo("c:\temp\").GetFileSystemInfos("*.mp3") _
order by file.LastWriteTime _
select file

EDIT4:这就是你要找的吗? 这将给你最大的。所有* .mp3文件的LastWriteTime日期。

(from file in new DirectoryInfo("c:\temp\").GetFileSystemInfos("*.mp3") _
order by file.LastWriteTime descending _
select file.LastWriteTime).Take(1)

OR

(from file in new DirectoryInfo("c:\temp\").GetFileSystemInfos("*.mp3") _
select file.LastWriteTime).Max()

答案 1 :(得分:0)

要完成此操作,请尝试以下操作...

设置实现IComparer接口的新类。这将用于执行比较。 IComparer提供了一种自定义集合排序顺序的方法。请注意,下面的示例使用LastWriteTime作为比较的基础,但是可以将其更改为您认为合适的属性。

Public Class clsCompareFileInfo
    Implements IComparer
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
        Dim File1 As FileInfo
        Dim File2 As FileInfo

        File1 = DirectCast(x, FileInfo)
        File2 = DirectCast(y, FileInfo)

        Compare = DateTime.Compare(File1.LastWriteTime, File2.LastWriteTime)
    End Function
End Class

然后,抓取文件集并执行以下操作以相应地对文件进行排序。

 Dim dirinfo As DirectoryInfo = New DirectoryInfo("C:\SearchFolder")
        Dim allFiles() As FileInfo = dirinfo.GetFiles("TheFileName*.txt", SearchOption.TopDirectoryOnly)
        Array.Sort(allFiles, New clsCompareFileInfo)
        For Each fl As FileInfo In allFiles
            MsgBox(fl.FullName.ToString())
        Next

答案 2 :(得分:0)

使用此类,您可以使用适当的条件订购文件(您必须为您需要的每个条件添加私人帮助程序类;-))


Imports System.IO

Class FilesTools
    Private Class HelperSortByLastWriteTimeAsc
        Implements IComparer(Of FileInfo)

        Public Function Compare(ByVal x As System.IO.FileInfo, _
                                ByVal y As System.IO.FileInfo) As Integer Implements System.Collections.Generic.IComparer(Of System.IO.FileInfo).Compare
            Return Date.Compare(x.LastWriteTime, y.LastWriteTime)
        End Function
    End Class

    Private Class HelperSortByLastWriteTimeDesc
        Implements IComparer(Of FileInfo)

        Public Function Compare(ByVal x As System.IO.FileInfo, _
                                ByVal y As System.IO.FileInfo) As Integer Implements System.Collections.Generic.IComparer(Of System.IO.FileInfo).Compare
            Return Date.Compare(y.LastWriteTime, x.LastWriteTime)
        End Function
    End Class

    Public Shared Function sortByLastTime() As IComparer(Of FileInfo)
        Return New HelperSortByLastWriteTimeAsc
    End Function

    Public Shared Function sortByLastTimeDesc() As IComparer(Of FileInfo)
        Return New HelperSortByLastWriteTimeDesc
    End Function

    Public Shared Function GetFilesSorted(ByVal path As String, _
                                          ByVal sort As IComparer(Of FileInfo)) As FileInfo()
        Dim info As FileInfo()
        info = New DirectoryInfo(path).GetFileSystemInfos()

        Array.Sort(Of FileInfo)(info, sort)

        Return info
    End Function

End Class