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?
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?
If you really want to do it with a recursive lambda expression here you go:
Note: this is probably several times slower (and way harder to read/debug/maintain) than the previous answers, but it does not stop if there is an I/O exception somewhere.
Reading files and directories is usually done with classes situated in the
System.IO
namespace. So the first step would consist into getting all the files that you need to read using the Directory.EnumerateFiles method and then for each file that corresponds to your search criteria read the contents using for example the File.ReadAllBytes method.Directory.GetFiles()
returns the full path of files that match the specified search pattern in the specified directory.Select
projects each element of fullpath sequence into a new form, only the filename.this returns just file names+extensions:
this returns just file names without extensions:
You don't need to use LINQ to do this - it's built into the framework:
or if you're using .NET 4:
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...