[英]How do I sort the filenames read with Directoryinfo
I'd like to read folder and file names that are thrown into one list sorted to show For example I have a file that looks like this: 我想读取被放入一个列表中的文件夹和文件名,这些列表被排序为显示例如我有一个如下所示的文件:
1-4999.txt 1-4999.txt
5000-9999.txt ...... 5000-9999.txt ......
I would like to sort the data folder 我想对数据文件夹进行排序
var endpageNext = pagenumber * 100;
var startpageNext = endpageNext - (100 - 1);
var lst = GetFile(startpageNext, endpageNext);
Picture Visual Studio break point 图片Visual Studio断点
internal List<string> GetFile(int startpagenext, int endpagenext)
{
var dir = DataAccessSetting.CustomerSplit;
DirectoryInfo directoryInfo = new DirectoryInfo(dir);
var s = directoryInfo.GetFiles("*.txt");
var lstfilename = new List<string>();
foreach (var file in s)
{
var fileWithoutExtension = Path.GetFileNameWithoutExtension(file.Name);
var splitNameFile = fileWithoutExtension.Split('-');
if ((startpagenext >= int.Parse(splitNameFile[0]) && startpagenext <= int.Parse(splitNameFile[1])) || (endpagenext >= int.Parse(splitNameFile[0]) && endpagenext <= int.Parse(splitNameFile[1])))
{
var ppath = dir + fileWithoutExtension + ".txt";
lstfilename.Add(ppath);
}
}
return lstfilename;
}
This should work: 这应该工作:
List<string> lstfilename = System.IO.Directory
.EnumerateFiles(dir, "*.txt", System.IO.SearchOption.TopDirectoryOnly)
.Select(Path => new {
Path,
split = System.IO.Path.GetFileNameWithoutExtension(Path).Split('-')
})
.Where(x => x.split.Length == 2 && x.split.All(s => s.All(Char.IsDigit)))
.Select(x => new {
x.Path,
Num1 = int.Parse(x.split[0]),
Num2 = int.Parse(x.split[1]),
})
.Where(x => (startpagenext >= x.Num1 && startpagenext <= x.Num2)
|| (endpagenext >= x.Num1 && endpagenext <= x.Num2))
.OrderBy(x => x.Num1).ThenBy(x => x.Num2)
.Select(x => x.Path)
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.