第一次写博客练习下
1 定义个文件比较类
public class FilesNameComparerClass :IComparer<string>
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
private static extern int StrCmpLogicalW(string psz1, string psz2);
public int Compare(string x, string y)
{
return StrCmpLogicalW(x, y);
}
}
在Windows XP以后,资源管理器的文件名默认排序准则就调用了StrCmpLogicalW。
C#中对的StrCmpLogicalW导入方法为:
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
private static extern int StrCmpLogicalW(string psz1, string psz2);
2. 调用
DirectoryInfo di = new DirectoryInfo(filePath);
FileInfo[] fileList = di.GetFiles("*.*");
fileList = fileList.OrderBy(s1 => s1.Name,new FilesNameComparerClass()).ToArray();
时间: 2024-10-13 20:27:16