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

前端 未结 20 2600
名媛妹妹
名媛妹妹 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:54

    A more efficient way of getting files with the extensions ".aspx" and ".ascx" that avoids querying the file system several times and avoids returning a lot of undesired files, is to pre-filter the files by using an approximate search pattern and to refine the result afterwards:

    var filteredFiles = Directory.GetFiles(path, "*.as?x")
        .Select(f => f.ToLowerInvariant())
        .Where(f => f.EndsWith("px") || f.EndsWith("cx"))
        .ToList();
    

提交回复
热议问题