c# – Return a list of Directory (Folder) names that match a partial string

c# – Return a list of Directory (Folder) names that match a partial string

You can use Linq SelectMany to parse your list of filters and return a list of the results with Directory.GetDirectories();
It will of course return all the Sub Directories that match the filter. Use just *.

public IEnumerable<string> GetFolders(string path, string[] filterStrings, SearchOption searchOption = SearchOption.AllDirectories)
{
    List<string> resultFolders = filterStrings
                 .SelectMany(flt => Directory.GetDirectories(path, flt, searchOption))
                 .ToList();
    return resultFolders;
}

try:

var patterns = new[] { Pictures_*, Videos_* };
var dirsFound = new List<string>();
foreach (var dir in patterns.Select(pattern => Directory.GetDirectories(@my path, pattern).ToArray()))
{
    dirsFound.AddRange(dir);
}

c# – Return a list of Directory (Folder) names that match a partial string

Looks like youre not looping through each of your filter strings:

var folders = new List<string>();
foreach (var filterString in filterStrings)
{
    folders.AddRange(Directory.EnumerateDirectories(path, filterString, searchOption););
}

Leave a Reply

Your email address will not be published. Required fields are marked *