如何使用LINQ从子文件夹递归读取文件名 [英] How to read File names recursively from subfolder using LINQ

查看:70
本文介绍了如何使用LINQ从子文件夹递归读取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用LINQ或LAMBDA表达式递归从目录及其子文件夹中读取扩展名为dll的文件名.

How to read file name with dll extension from a directory and from its subfolders recursively using LINQ or LAMBDA expression.

现在我正在使用嵌套的for-each循环来执行此操作. 有什么方法可以使用LINQ或LAMBDA表达式吗?

Now i'm using Nested for-each loop to do this. Is there any way to do this using LINQ or LAMBDA expression?

推荐答案

您无需使用LINQ即可-它是

You don't need to use LINQ to do this - it's built into the framework:

string[] files = Directory.GetFiles(directory, "*.dll",
                                    SearchOption.AllDirectories);

如果您使用的是.NET 4 :

IEnumerable<string> files = Directory.EnumerateFiles(directory, "*.dll",
                                                    SearchOption.AllDirectories);

说实话,LINQ在递归方面并不出色.您可能想要编写自己的通用递归扩展方法.考虑到经常问这种问题的次数,我应该自己动一点时间……

To be honest, LINQ isn't great in terms of recursion. You'd probably want to write your own general-purpose recursive extension method. Given how often this sort of question is asked, I should really do that myself some time...

这篇关于如何使用LINQ从子文件夹递归读取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆