System.IO.Directory.GetFiles 的多个文件扩展名 searchPattern
将多个文件扩展名设置为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 不是一个选项,它必须是传递到 GetFiles
中的 searchPattern
,如问题。
What is the syntax for setting multiple file-extensions as searchPattern
on Directory.GetFiles()
? For example filtering out files with .aspx and .ascx extensions.
// TODO: Set the string 'searchPattern' to only get files with
// the extension '.aspx' and '.ascx'.
var filteredFiles = Directory.GetFiles(path, searchPattern);
Update: LINQ is not an option, it has to be a searchPattern
passed into GetFiles
, as specified in the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
编辑 2014-07-23
您可以在 .NET 4.5 中执行此操作以获得更快的枚举:
MSDN 中的 Directory.EnumerateFiles
Edit 2014-07-23
You can do this in .NET 4.5 for a faster enumeration:
Directory.EnumerateFiles in MSDN
我喜欢这种方法,因为它具有可读性并且避免了目录的多次迭代:
I like this method, because it is readable and avoids multiple iterations of the directory:
我相信没有“开箱即用”的解决方案,这是 Directory.GetFiles 方法的限制。
不过,编写自己的方法相当容易,这里有一个示例。
代码可以是:
I believe there is no "out of the box" solution, that's a limitation of the Directory.GetFiles method.
It's fairly easy to write your own method though, here is an example.
The code could be:
GetFiles 只能匹配单个模式,但您可以使用 Linq 调用具有多个模式的 GetFiles:
请参阅此处的注释部分:
http://www.codeproject.com/KB/aspnet/NET_DirectoryInfo.aspx
GetFiles can only match a single pattern, but you can use Linq to invoke GetFiles with multiple patterns:
See comments section here:
http://www.codeproject.com/KB/aspnet/NET_DirectoryInfo.aspx
Directory.EnumerateFiles
来提高性能(Directory.EnumerateFiles 与 Directory.GetFiles 之间有什么区别?)或者,分割和合并你的 glob 可能会更快(至少看起来更干净):
Directory.EnumerateFiles
for a performance boost (What is the difference between Directory.EnumerateFiles vs Directory.GetFiles?)Or, it may be faster to split and merge your globs (at least it looks cleaner):
我担心你将不得不做这样的事情,我从这里改变了正则表达式< /a>.
I fear you will have to do somthing like this, I mutated the regex from here.
易于记忆、懒惰且可能不完美的解决方案:
The easy-to-remember, lazy and perhaps imperfect solution:
我将使用以下内容:
编辑:更正了目录和目录信息之间的不匹配
I would use the following:
EDIT: corrected due mismatch between Directory and DirectoryInfo
我会尝试指定一些类似
它应该起作用的东西。
I would try to specify something like
it should work.
获取扩展名为“.aspx”和“.ascx”的文件的更有效方法
避免多次查询文件系统并避免返回大量不需要的文件的方法是使用近似搜索模式预先过滤文件,然后细化结果:
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:
你可以这样做
You can do it like this
我会选择使用 Path.GetExtension() 方法,而不是 EndsWith 函数。这是完整的示例:
或者:(
如果您关心性能,请使用
StringComparison.OrdinalIgnoreCase
:MSDN 字符串比较)Instead of the EndsWith function, I would choose to use the
Path.GetExtension()
method instead. Here is the full example:or:
(Use
StringComparison.OrdinalIgnoreCase
if you care about performance: MSDN string comparisons)看起来像这个演示:
look like this demo:
@Daniel B,感谢您编写我自己的此函数版本的建议。它与 Directory.GetFiles 具有相同的行为,但支持正则表达式过滤。
我发现它很有用,所以我想分享一下。
@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.
I found it useful, so I thought I'd share.
@qfactor77 答案的 c# 版本。这是没有 LINQ 的最好方法。
现在返回
filePath
字符串数组。一开始您还需要添加对
Microsoft.VisualBasic
的引用c# version of @qfactor77's answer. This is the best way without LINQ .
now return
filePath
string array. In the beginning you needalso you need to add reference to
Microsoft.VisualBasic
我做了一个简单的方法来搜索您需要的尽可能多的扩展,并且没有 ToLower()、RegEx、foreach...
致力于 .Net Standard 2.0。
I did a simple way for seach as many extensions as you need, and with no ToLower(), RegEx, foreach...
Working on .Net Standard 2.0.
只是想说,如果您使用
FileIO.FileSystem.GetFiles
而不是Directory.GetFiles
,它将允许使用通配符数组。例如:
Just would like to say that if you use
FileIO.FileSystem.GetFiles
instead ofDirectory.GetFiles
, it will allow an array of wildcards.For example:
(很抱歉将此作为答案,但我还没有撰写评论的权限。)
请注意,FileIO.FileSystem.GetFiles() 方法只是一个包装器,用于对每个提供的模式执行搜索并合并结果。
从 .pbd 文件检查源代码时,您可以从此片段中看到 FileSystem.FindPaths 对集合中的每个模式执行:
(Sorry to write this as an answer, but I don't have privileges to write comments yet.)
Note that the FileIO.FileSystem.GetFiles() method from Microsoft.VisualBasic is just a wrapper to execute a search for each provided pattern and merge the results.
When checking the source from the .pbd file, you can see from this fragment FileSystem.FindPaths is executed for each pattern in the collection:
根据 jonathan 的回答(对于 2 个文件扩展名):
或者更多文件扩展名(在此文件夹和子文件夹中搜索) :
在 3250 个文件中查找 1890 个文件需要 0.6 秒。
According to jonathan's answer (for 2 file extensions):
Or for more file extensions (search in this folder and subfolders):
In 3250 files took to find 1890 files takes 0.6 second.