http://bbs.csdn.net/topics/391037085
使用.NET默认的 GetFiles 函数,如果文件中有数字,比如 1.xxx 2.xxx …… 10.xxx
那么文件的默认排序就会变成 1.xxx 10.xxx 2.xxx ...
而不会是按照数字来进行排序
下面介绍如何按照 Explorer 的规则来进行文件排序
API声明
<Runtime.InteropServices.DllImport("Shlwapi.dll", CharSet:=Runtime.InteropServices.CharSet.Unicode)> _
Shared Function StrCmpLogicalW(ByVal psz1 As String, ByVal psz2 As String) As Integer
End Function
使用方法
Dim files As String() = {"1.jpg", "2.jpg", "3.jpg", "10.jpg", "11.jpg", "20.jpg", "30.jpg"}
Array.Sort(files, AddressOf StrCmpLogicalW)
For Each file As String In files
Console.WriteLine(file)
Next
最后
优化组合一下
''' <summary>文件排序规则API</summary>
<Runtime.InteropServices.DllImport("Shlwapi.dll", CharSet:=Runtime.InteropServices.CharSet.Unicode)> _
Shared Function StrCmpLogicalW(ByVal psz1 As String, ByVal psz2 As String) As Integer
End Function
Shared Function FileInfoCmpLogicalW(ByVal fi1 As FileInfo, ByVal fi2 As FileInfo) As Integer
Return StrCmpLogicalW(fi1.FullName, fi2.FullName)
End Function
''' <summary>对文件进行排序</summary>
Shared Sub FilesSort(ByVal files As String())
Array.Sort(files, AddressOf StrCmpLogicalW)
End Sub
Shared Sub FilesSort(ByVal filesinfo As FileInfo())
'Array.Sort(Of FileInfo)(filesinfo, AddressOf FileInfoCmpLogicalW)
Array.Sort(filesinfo, AddressOf FileInfoCmpLogicalW)
End Sub