GetFiles 排序

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim di As New IO.DirectoryInfo("C:\Temp\Test")
        Dim files = di.GetFiles
        Array.Sort(files, New FileNameComparer)

        ListBox1.DataSource = files
    End Sub

    Public Class FileNameComparer
        Implements IComparer

        Dim reD As New Regex("\d+")
        Dim reS As New Regex("\D+")

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            Dim fi1 As IO.FileInfo = DirectCast(x, IO.FileInfo)
            Dim fi2 As IO.FileInfo = DirectCast(y, IO.FileInfo)

            If fi1.Name = fi2.Name Then
                Return 0
            ElseIf reD.IsMatch(fi1.Name) AndAlso reD.IsMatch(fi2.Name) Then
                Dim fi1m = reS.Match(fi1.Name).Value & reD.Match(fi1.Name).Value.PadLeft(20, "0") & fi1.Name
                Dim fi2m = reS.Match(fi2.Name).Value & reD.Match(fi2.Name).Value.PadLeft(20, "0") & fi2.Name
                If fi1m > fi2m Then Return 1
                If fi1m < fi2m Then Return -1
            Else
                If fi1.Name > fi2.Name Then Return 1
                If fi1.Name < fi2.Name Then Return -1
            End If
        End Function
    End Class

https://www.vbforums.com/showthread.php?693729-R ... etFiles()-sort-files-by-name





Please follow WeChat's public account ByCAD