以下是我的尝试:
var vv = new DirectoryInfo(@"C:\Image").GetFileSystemInfos("*.bmp").OrderBy(fs=>int.Parse(fs.Name.Split('_')[1].Substring(0, fs.Name.Split('_')[1].Length - fs.Extension.Length)));
答案 0 :(得分:0)
我建议您创建一个自定义排序器类,它实现IComparer接口以对FileSystemInfo数组进行排序。
是的,它不像使用LINQ那样性感,但它很干净
DirectoryInfo di = new DirectoryInfo("C:\\Image");
FileSystemInfo[] fi = di.GetFileSystemInfos("*.bmp");
Array.Sort(fi, new SortByNum());
public class SortByNum : IComparer<FileSystemInfo>
{
public int Compare( FileSystemInfo x, FileSystemInfo y)
{
// extract the numeric portion of the file name
int val_x = int.Parse(x.Name.Split('_')[1].Substring(0, x.Name.Split('_')[1].Length - x.Extension.Length));
int val_y = int.Parse(y.Name.Split('_')[1].Substring(0, y.Name.Split('_')[1].Length - y.Extension.Length));
return val_x.CompareTo(val_y);
}
}