文件夹目录排序

using System;
using System.Collections.Generic;

namespace Microsoft.Xna.User.GraphicsDeviceUserControl
{
  public class FilesNameSort : IComparer<string>
  {
    [System.Runtime.InteropServices.DllImport("Shlwapi.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
    public static extern int StrCmpLogicalW(string str1, string str2);
    public int Compare(string str1, string str2)
    {
      return StrCmpLogicalW(str1, str2);
    }
  }
  public partial class MyForm : Form
  {
    public MyForm()
    {
      InitializeComponent();
    }

    private void Button_Click(object sender, EventArgs e)
    {

      FolderBrowserDialog dialog = new FolderBrowserDialog();

      if (dialog.ShowDialog() == DialogResult.OK)
      {
        string strPath = dialog.SelectedPath;

        DirectoryInfo theFolder = new DirectoryInfo(strPath);

        FileInfo[] theFiles = theFolder.GetFiles("*.xnb");
        theFiles = theFiles.OrderBy(y => y.Name, new FilesNameSort()).ToArray();

        string[] files = new string[theFiles.Length];

        for (int i = 0; i < files.Count(); i++)
        {
          files[i] = Path.GetFileNameWithoutExtension(theFiles[i].FullName);
        }
      }
    }
  }
}

支持个人观看使用,如商用或转载,请告知! -----萧朗(QQ:453929789 Email:xiaolang_xl@sina.com)
原文地址:https://www.cnblogs.com/XiaoLang0/p/12133816.html