Multiple file-extensions searchPattern for System.IO.Directory.GetFiles

前端 未结 20 2329
名媛妹妹
名媛妹妹 2020-11-27 11:08

What is the syntax for setting multiple file-extensions as searchPattern on Directory.GetFiles()? For example filtering out files

相关标签:
20条回答
  • 2020-11-27 11:58

    @Daniel B, thanks for the suggestion to write my own version of this function. It has the same behavior as Directory.GetFiles, but supports regex filtering.

    string[] FindFiles(FolderBrowserDialog dialog, string pattern)
        {
            Regex regex = new Regex(pattern);
    
            List<string> files = new List<string>();
            var files=Directory.GetFiles(dialog.SelectedPath);
            for(int i = 0; i < files.Count(); i++)
            {
                bool found = regex.IsMatch(files[i]);
                if(found)
                {
                    files.Add(files[i]);
                }
            }
    
            return files.ToArray();
        }
    

    I found it useful, so I thought I'd share.

    0 讨论(0)
  • 2020-11-27 11:58
    var filtered = Directory.GetFiles(path)
        .Where(file => file.EndsWith("aspx", StringComparison.InvariantCultureIgnoreCase) || file.EndsWith("ascx", StringComparison.InvariantCultureIgnoreCase))
        .ToList();
    
    0 讨论(0)
提交回复
热议问题