How to read File names recursively from subfolder using LINQ

前端 未结 5 1179
鱼传尺愫
鱼传尺愫 2021-01-13 09:36

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

5条回答
  •  再見小時候
    2021-01-13 10:21

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

提交回复
热议问题