Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read File names recursively from subfolder using LINQ

Tags:

c#

lambda

linq

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

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

like image 315
Thorin Oakenshield Avatar asked Sep 22 '10 11:09

Thorin Oakenshield


1 Answers

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

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

or if you're using .NET 4:

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

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...

like image 58
Jon Skeet Avatar answered Oct 03 '22 00:10

Jon Skeet