Reputation: 111
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
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
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;
}
Upvotes: 3
Views: 94
Reputation: 460268
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();
Upvotes: 2