Linq to 文件系统获取每个子文件夹中最后创建的具有不同扩展名的文件

发布于 2024-12-27 17:45:08 字数 286 浏览 3 评论 0原文

请参阅我的帖子

在这里,我在每个子文件夹中获取最新创建的文件,但我需要获取具有不同扩展名的最新创建的文件。

例如,有五个子文件夹,每个子文件夹包含多个 PDF 文件和多个 Excel 文件,那么查询应从每个目录中带入一个 PDF 文件和一个 Excel 文件。结果中应该有十项。

see my post.

Here I am getting latest created file in each sub folder, but I need to get the latest created files with different extensions.

For example, there are five sub folders each containing more than one PDF file and more than one Excel file, then the query should bring one PDF file and one Excel file from each directory. I should have ten items in the result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2025-01-03 17:45:08

这对你有用吗?

DirectoryInfo di = new DirectoryInfo(@"C:\Installers");

var recentFiles = di.GetDirectories()
    .SelectMany(d => d.EnumerateFiles()
                      .GroupBy(f => f.Extension)
                      .Select(gf => gf.OrderByDescending(f => f.CreationTimeUtc)
                                      .FirstOrDefault()))
    .Where(x => x != null)
    .Select(x => x.FullName)
    .ToList();

Would this work for you?

DirectoryInfo di = new DirectoryInfo(@"C:\Installers");

var recentFiles = di.GetDirectories()
    .SelectMany(d => d.EnumerateFiles()
                      .GroupBy(f => f.Extension)
                      .Select(gf => gf.OrderByDescending(f => f.CreationTimeUtc)
                                      .FirstOrDefault()))
    .Where(x => x != null)
    .Select(x => x.FullName)
    .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文