0 Replies - 174 Views - Last Post: 20 February 2010 - 11:58 PM

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Search a directory with multiple search types with LINQ

Posted 20 February 2010 - 11:58 PM

Description: Need reference:

System.Collections.Generic
System.Linq
System.IOSince there's no way in .Net (as of yet) to search for multiple file types I came up with a way to accomplish it. Not only that but it doesnt seem to sppear to be a bigger hit than the normal Directory.GetFiles
/// <summary>
///  method for retrieving files from a directory using LINQ
/// </summary>
/// <param name="directory">directory we want searched</param>
/// <param name="patterns">search patterns we're using</param>
/// <returns>list of matching files</returns>
public List<string> GetFiles(string dir, List<string> patterns)
{
    List<string> matches = new List<string>();

    //loop thorugh all extensions provided
    foreach (string pattern in patterns)
    {
        //use LINQ to get each file with the soecified file type
        var matchingFiles = from file in Directory.GetFiles(dir, pattern)
                    select file;

        //now add all files to our list
        matches.AddRange(matchingFiles);
    }

    return matches;
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1