给定是包含的目录 AA20180501_1.txt ,AA20180501_3.txt ... ,AA20180501_(N)的.txt ,AA20180502_1.txt ... ,AA20180502_(N)的.txt 等等。
我需要将每天最高(_n)的文件添加到arraylist中,我需要在C#中进行。
我是否提到我对C#的了解充其量只是粗略的? 任何想法和建议都非常感谢。
答案 0 :(得分:2)
请尝试这个简单的解决方案......
DirectoryInfo directory = new DirectoryInfo(@"D:\Temp\");
var files = directory.GetFiles("*.txt")
.OrderBy(x => x.Name)
.GroupBy(x => x.Name.Substring(2, 8))
.Select(x => x.Last())
.ToArray();
答案 1 :(得分:1)
随意将文件扩展名更改为您需要的文件扩展名
string pattern = "(?i)([a-z]+[0-9]+)_([0-9]+).txt";
var his =
from file in Directory.EnumerateFiles(@"C:\Temp\test\", "*.txt")
let match = Regex.Match(file, pattern)
where match.Success
let info = new { Name = match.Groups[1].Value, Num = match.Groups[2].Value }
group info by info.Name into g
select new { FileName = g.Key, MaxNum = g.Max(x => x.Num) };
foreach(var hi in his)
{
WriteLine($"File name '{hi.FileName}' has max num: '{hi.MaxNum}'");
}