Last Updated: February 25, 2016
·
732
· lucidquiet

Linq Collecting Files Names Recursively

I use this snippet of code to gather paths off the hard-drive. Same pattern works for many other tree-traversals.

public static IEnumerable<string> Files(
    this string root, Func<string, bool> accept)
{
    return
    Directory.GetFiles(root).Where(accept)
        .Concat(
            Directory.GetDirectories(root)
                .SelectMany(d => d.Files(accept)));
}

As an extensions it works this way:

@"\Sumblime 2\Packages".Files(
    s => s.EndsWith("tmLanguage");

Cheers