当前位置: 代码迷 >> 综合 >> API(11)——C# 读取文件夹下文件,按照名字排序
  详细解决方案

API(11)——C# 读取文件夹下文件,按照名字排序

热度:16   发布时间:2023-10-01 14:32:23.0

 

正常的读取文件时非常简单的,按照文件名排序就要参照Windows的排序,往往添加排序处理后与windows的排序规则不统一,所以要学会调用windows的排序dll,可以节省很多时间。

//APP_DATA\AdvertisementDirectoryInfo folder = new DirectoryInfo("./APP_DATA/Advertisement");if (folder.Exists){FileInfo[] files = folder.GetFiles();// 文件名的升序Array.Sort(files, new FileNameSort());}

文件的排序功能,目测sort使用的是冒泡排序:

    public class FileNameSort : IComparer{//调用DLL[System.Runtime.InteropServices.DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)]private static extern int StrCmpLogicalW(string param1, string param2);//前后文件名进行比较。public int Compare(object name1, object name2){if (null == name1 && null == name2){return 0;}if (null == name1){return -1;}if (null == name2){return 1;}return StrCmpLogicalW(name1.ToString(), name2.ToString());}}

 

  相关解决方案