多个文件扩展名搜索 System.IO.Directory.GetFiles 的模式

本文关键字:Directory GetFiles 模式 IO System 文件 扩展名 搜索 | 更新日期: 2023-09-27 18:05:28

多个文件扩展名设置为Directory.GetFiles() searchPattern的语法是什么?例如,筛选出扩展名为 .aspx.ascx 的文件。

// TODO: Set the string 'searchPattern' to only get files with
// the extension '.aspx' and '.ascx'.
var filteredFiles = Directory.GetFiles(path, searchPattern);

更新LINQ 不是一个选项,它必须是传递到 GetFilessearchPattern,如问题中所述。

多个文件扩展名搜索 System.IO.Directory.GetFiles 的模式

var filteredFiles = Directory
    .GetFiles(path, "*.*")
    .Where(file => file.ToLower().EndsWith("aspx") || file.ToLower().EndsWith("ascx"))
    .ToList();

编辑 2014-07-23

可以在 .NET 4.5 中执行此操作以加快枚举速度:

var filteredFiles = Directory
    .EnumerateFiles(path) //<--- .NET 4.5
    .Where(file => file.ToLower().EndsWith("aspx") || file.ToLower().EndsWith("ascx"))
    .ToList();

MSDN 中的 Directory.EnumerateFiles

我喜欢这种方法,因为它是可读的,并且避免了目录的多次迭代:

var allowedExtensions = new [] {".doc", ".docx", ".pdf", ".ppt", ".pptx", ".xls", ".xslx"}; 
var files = Directory
    .GetFiles(folder)
    .Where(file => allowedExtensions.Any(file.ToLower().EndsWith))
    .ToList();
我相信

没有"开箱即用"的解决方案,这是Directory.GetFiles方法的限制。

不过,编写自己的方法相当容易,这里有一个例子。

代码可以是:

/// <summary>
/// Returns file names from given folder that comply to given filters
/// </summary>
/// <param name="SourceFolder">Folder with files to retrieve</param>
/// <param name="Filter">Multiple file filters separated by | character</param>
/// <param name="searchOption">File.IO.SearchOption, 
/// could be AllDirectories or TopDirectoryOnly</param>
/// <returns>Array of FileInfo objects that presents collection of file names that 
/// meet given filter</returns>
public string[] getFiles(string SourceFolder, string Filter, 
 System.IO.SearchOption searchOption)
{
 // ArrayList will hold all file names
ArrayList alFiles = new ArrayList();
 // Create an array of filter string
 string[] MultipleFilters = Filter.Split('|');
 // for each filter find mathing file names
 foreach (string FileFilter in MultipleFilters)
 {
  // add found file names to array list
  alFiles.AddRange(Directory.GetFiles(SourceFolder, FileFilter, searchOption));
 }
 // returns string array of relevant file names
 return (string[])alFiles.ToArray(typeof(string));
}

GetFiles 只能匹配一种模式,但您可以使用 Linq 调用具有多种模式的 GetFiles:

FileInfo[] fi = new string[]{"*.txt","*.doc"}
    .SelectMany(i => di.GetFiles(i, SearchOption.AllDirectories))
    .ToArray();

请参阅此处的评论部分:http://www.codeproject.com/KB/aspnet/NET_DirectoryInfo.aspx

var filteredFiles = Directory
    .EnumerateFiles(path, "*.*") // .NET4 better than `GetFiles`
    .Where(
        // ignorecase faster than tolower...
        file => file.ToLower().EndsWith("aspx")
        || file.EndsWith("ascx", StringComparison.OrdinalIgnoreCase))
    .ToList();
  • 不要忘记新的.NET4 Directory.EnumerateFiles用于提高性能(Directory.EnumerateFiles 与 Directory.GetFiles 有什么区别?
  • "IgnoreCase"应该比"ToLower"更快

或者,拆分和合并您的球体可能会更快(至少它看起来更干净(:

"*.ext1;*.ext2".Split(';')
    .SelectMany(g => Directory.EnumerateFiles(path, g))
    .ToList();

我担心你将不得不做这样的事情,我从这里改变了正则表达式。

var searchPattern = new Regex(
    @"$(?<='.(aspx|ascx))", 
    RegexOptions.IgnoreCase);
var files = Directory.EnumerateFiles(path)
    .Where(f => searchPattern.IsMatch(f))
    .ToList();

容易记住、懒惰且可能不完美的解决方案:

Directory.GetFiles(dir, "*.dll").Union(Directory.GetFiles(dir, "*.exe"))

我会使用以下方法:

var ext = new string[] { ".ASPX", ".ASCX" };
FileInfo[] collection = (from fi in new DirectoryInfo(path).GetFiles()
                         where ext.Contains(fi.Extension.ToUpper())
                         select fi)
                         .ToArray();

编辑:由于目录和目录信息之间的不匹配而更正

我会尝试指定类似的东西

var searchPattern = "as?x";

它应该有效。

获取

扩展名为".aspx"和".ascx"的文件的更有效方法这样可以避免多次查询文件系统并避免返回大量不需要的文件,方法是使用近似搜索模式预先过滤文件,然后优化结果:

var filteredFiles = Directory.GetFiles(path, "*.as?x")
    .Select(f => f.ToLowerInvariant())
    .Where(f => f.EndsWith("px") || f.EndsWith("cx"))
    .ToList();
    /// <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();
    }
    public static bool CheckFiles(string pathA, string pathB)
    {
        string[] extantionFormat = new string[] { ".war", ".pkg" };
        return CheckFiles(pathA, pathB, extantionFormat);
    }
    public static bool CheckFiles(string pathA, string pathB, string[] extantionFormat)
    {
        System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA);
        System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);
        // Take a snapshot of the file system. list1/2 will contain only WAR or PKG 
        // files
        // fileInfosA will contain all of files under path directories 
        FileInfo[] fileInfosA = dir1.GetFiles("*.*", 
                              System.IO.SearchOption.AllDirectories);
        // list will contain all of files that have ..extantion[]  
        // Run on all extantion in extantion array and compare them by lower case to 
        // the file item extantion ...
        List<System.IO.FileInfo> list1 = (from extItem in extantionFormat
                                          from fileItem in fileInfosA
                                          where extItem.ToLower().Equals 
                                          (fileItem.Extension.ToLower())
                                          select fileItem).ToList();
        // Take a snapshot of the file system. list1/2 will contain only WAR or  
        // PKG files
        // fileInfosA will contain all of files under path directories 
        FileInfo[] fileInfosB = dir2.GetFiles("*.*", 
                                       System.IO.SearchOption.AllDirectories);
        // list will contain all of files that have ..extantion[]  
        // Run on all extantion in extantion array and compare them by lower case to 
        // the file item extantion ...
        List<System.IO.FileInfo> list2 = (from extItem in extantionFormat
                                          from fileItem in fileInfosB
                                          where extItem.ToLower().Equals            
                                          (fileItem.Extension.ToLower())
                                          select fileItem).ToList();
        FileCompare myFileCompare = new FileCompare();
        // This query determines whether the two folders contain 
        // identical file lists, based on the custom file comparer 
        // that is defined in the FileCompare class. 
        return list1.SequenceEqual(list2, myFileCompare);
    }

而不是EndsWith 函数,我会选择使用 Path.GetExtension() 方法。以下是完整示例:

var filteredFiles = Directory.EnumerateFiles( path )
.Where(
    file => Path.GetExtension(file).Equals( ".aspx", StringComparison.OrdinalIgnoreCase ) ||
            Path.GetExtension(file).Equals( ".ascx", StringComparison.OrdinalIgnoreCase ) );

或:

var filteredFiles = Directory.EnumerateFiles(path)
.Where(
    file => string.Equals( Path.GetExtension(file), ".aspx", StringComparison.OrdinalIgnoreCase ) ||
            string.Equals( Path.GetExtension(file), ".ascx", StringComparison.OrdinalIgnoreCase ) );

(如果您关心性能,请使用StringComparison.OrdinalIgnoreCase:MSDN 字符串比较(

你可以这样做

new DirectoryInfo(path).GetFiles().Where(Current => Regex.IsMatch(Current.Extension, "''.(aspx|ascx)", RegexOptions.IgnoreCase)

看起来像这个演示:

void Main()
{
    foreach(var f in GetFilesToProcess("c:''", new[] {".xml", ".txt"}))
        Debug.WriteLine(f);
}
private static IEnumerable<string> GetFilesToProcess(string path, IEnumerable<string> extensions)
{
   return Directory.GetFiles(path, "*.*")
       .Where(f => extensions.Contains(Path.GetExtension(f).ToLower()));
}

@Daniel B,感谢您建议编写我自己的函数版本。它与 Directory.GetFiles 具有相同的行为,但支持正则表达式筛选。

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();
    }

我发现它很有用,所以我想我会分享。

@qfactor77答案的C#版本。这是没有 LINQ 的最佳方法。

string[] wildcards= {"*.mp4", "*.jpg"};
ReadOnlyCollection<string> filePathCollection = FileSystem.GetFiles(dirPath, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, wildcards);
string[] filePath=new string[filePathCollection.Count];
filePathCollection.CopyTo(filePath,0);

现在返回filePath字符串数组。一开始你需要

using Microsoft.VisualBasic.FileIO;
using System.Collections.ObjectModel;

您还需要添加对Microsoft.VisualBasic的引用

我做了一个简单的方法来搜索尽可能多的扩展,并且没有ToLower((,RegEx,foreach...

List<String> myExtensions = new List<String>() { ".aspx", ".ascx", ".cs" }; // You can add as many extensions as you want.
DirectoryInfo myFolder = new DirectoryInfo(@"C:'FolderFoo");
SearchOption option = SearchOption.TopDirectoryOnly; // Use SearchOption.AllDirectories for seach in all subfolders.
List<FileInfo> myFiles = myFolder.EnumerateFiles("*.*", option)
    .Where(file => myExtensions
    .Any(e => String.Compare(file.Extension, e, CultureInfo.CurrentCulture, CompareOptions.IgnoreCase) == 0))
    .ToList();

在 .Net 标准 2.0 上工作。

var filtered = Directory.GetFiles(path)
    .Where(file => file.EndsWith("aspx", StringComparison.InvariantCultureIgnoreCase) || file.EndsWith("ascx", StringComparison.InvariantCultureIgnoreCase))
    .ToList();

只是想说,如果您使用 FileIO.FileSystem.GetFiles 而不是 Directory.GetFiles ,它将允许使用通配符数组。

例如:

Dim wildcards As String() = {"*.html", "*.zip"}
Dim ListFiles As List(Of String) = FileIO.FileSystem.GetFiles(directoryyouneed, FileIO.SearchOption.SearchTopLevelOnly, wildcards).ToList

(很抱歉写这个作为答案,但我还没有写评论的权限。

请注意,来自 Microsoft.VisualBasic 的 FileIO.FileSystem.GetFiles(( 方法只是一个包装器,用于对每个提供的模式执行搜索并合并结果。从 .pbd 文件检查源代码时,可以从此片段中看到 FileSystem.FindPath 针对集合中的每个模式执行:

private static void FindFilesOrDirectories(
  FileSystem.FileOrDirectory FileOrDirectory,
  string directory,
  SearchOption searchType,
  string[] wildcards,
  Collection<string> Results)
{
    // (...)
    string[] strArray = wildcards;
    int index = 0;
    while (index < strArray.Length)
    {
      string wildCard = strArray[index];
      FileSystem.AddToStringCollection(Results, FileSystem.FindPaths(FileOrDirectory, directory, wildCard));
      checked { ++index; }
    }
    // (...)
}

根据乔纳森的回答(对于 2 个文件扩展名(:

public static string[] GetFilesList(string dir) =>
    Directory.GetFiles(dir, "*.exe", SearchOption.AllDirectories)
    .Union(Directory.GetFiles(dir, "*.dll", SearchOption.AllDirectories)).ToArray();
    

或者更多文件扩展名(在此文件夹和子文件夹中搜索(:

public static List<string> GetFilesList(string dir, params string[] fileExtensions) {
    List<string> files = new List<string>();
    foreach (string fileExtension in fileExtensions) {
        files.AddRange(Directory.GetFiles(dir, fileExtension, SearchOption.AllDirectories));
    }
    return files;
}
List<string> files = GetFilesList("C:''", "*.exe", "*.dll");

在 3250 个文件中查找 1890 个文件需要 0.6 秒。