string数组根据名称排序

  DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
  string[] filesArray = Directory.GetFiles(directoryPath, "*.*", SearchOption.TopDirectoryOnly);
  Array.Sort(filesArray, new FileNameSort());
using System.Collections;
using System.Runtime.InteropServices;

public class FileNameSort : IComparer
{
    
    
    //调用windos 的 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());
    }


}

猜你喜欢

转载自blog.csdn.net/qq_42223582/article/details/128110485