Can you call Directory.GetFiles() with multiple filters?

前端 未结 26 2434
逝去的感伤
逝去的感伤 2020-11-22 05:25

I am trying to use the Directory.GetFiles() method to retrieve a list of files of multiple types, such as mp3\'s and jpg\'s. I have t

相关标签:
26条回答
  • 2020-11-22 05:54

    I can't use .Where method because I'm programming in .NET Framework 2.0 (Linq is only supported in .NET Framework 3.5+).

    Code below is not case sensitive (so .CaB or .cab will be listed too).

    string[] ext = new string[2] { "*.CAB", "*.MSU" };
    
    foreach (string found in ext)
    {
        string[] extracted = Directory.GetFiles("C:\\test", found, System.IO.SearchOption.AllDirectories);
    
        foreach (string file in extracted)
        {
            Console.WriteLine(file);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:55
    /// <summary>
    /// Returns the names of files in a specified directories that match the specified patterns using LINQ
    /// </summary>
    /// <param name="srcDirs">The directories to seach</param>
    /// <param name="searchPatterns">the list of search patterns</param>
    /// <param name="searchOption"></param>
    /// <returns>The list of files that match the specified pattern</returns>
    public static string[] GetFilesUsingLINQ(string[] srcDirs,
         string[] searchPatterns,
         SearchOption searchOption = SearchOption.AllDirectories)
    {
        var r = from dir in srcDirs
                from searchPattern in searchPatterns
                from f in Directory.GetFiles(dir, searchPattern, searchOption)
                select f;
    
        return r.ToArray();
    }
    
    0 讨论(0)
  • 2020-11-22 05:57

    For .NET 4.0 and later,

    var files = Directory.EnumerateFiles("C:\\path", "*.*", SearchOption.AllDirectories)
                .Where(s => s.EndsWith(".mp3") || s.EndsWith(".jpg"));
    

    For earlier versions of .NET,

    var files = Directory.GetFiles("C:\\path", "*.*", SearchOption.AllDirectories)
                .Where(s => s.EndsWith(".mp3") || s.EndsWith(".jpg"));
    

    edit: Please read the comments. The improvement that Paul Farry suggests, and the memory/performance issue that Christian.K points out are both very important.

    0 讨论(0)
  • 2020-11-22 05:57

    Just found an another way to do it. Still not one operation, but throwing it out to see what other people think about it.

    private void getFiles(string path)
    {
        foreach (string s in Array.FindAll(Directory.GetFiles(path, "*", SearchOption.AllDirectories), predicate_FileMatch))
        {
            Debug.Print(s);
        }
    }
    
    private bool predicate_FileMatch(string fileName)
    {
        if (fileName.EndsWith(".mp3"))
            return true;
        if (fileName.EndsWith(".jpg"))
            return true;
        return false;
    }
    
    0 讨论(0)
  • 2020-11-22 05:58

    Nope. Try the following:

    List<string> _searchPatternList = new List<string>();
        ...
        List<string> fileList = new List<string>();
        foreach ( string ext in _searchPatternList )
        {
            foreach ( string subFile in Directory.GetFiles( folderName, ext  )
            {
                fileList.Add( subFile );
            }
        }
    
        // Sort alpabetically
        fileList.Sort();
    
        // Add files to the file browser control    
        foreach ( string fileName in fileList )
        {
            ...;
        }
    

    Taken from: http://blogs.msdn.com/markda/archive/2006/04/20/580075.aspx

    0 讨论(0)
  • 2020-11-22 05:59

    I know it's old question but LINQ: (.NET40+)

    var files = Directory.GetFiles("path_to_files").Where(file => Regex.IsMatch(file, @"^.+\.(wav|mp3|txt)$"));
    
    0 讨论(0)
提交回复
热议问题