查找文件数量具有特定扩展名,在所有子目录 [英] Find number of files with a specific extension, in all subdirectories

查看:182
本文介绍了查找文件数量具有特定扩展名,在所有子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找到一个特定类型的文件的数量,而无需通过所有结果客栈Directory.GetFiles()或类似的方法有循环?我期待这样的事情:

Is there a way to find the number of files of a specific type without having to loop through all results inn a Directory.GetFiles() or similar method? I am looking for something like this:

int ComponentCount = MagicFindFileCount(@"c:\windows\system32", "*.dll");

我知道我可以做一个递归函数来调用Directory.GetFiles,但它是干净多了,如果我能做到这一点没有所有的迭代。

I know that I can make a recursive function to call Directory.GetFiles , but it would be much cleaner if I could do this without all the iterating.

编辑:如果这是不可能做到这一点没有自己递归迭代和,这将是最好的方式做到这一点。

If it is not possible to do this without recursing and iterating yourself, what would be the best way to do it?

推荐答案

您应该使用的 Directory.GetFiles(路径,是searchPattern,SearchOption) Directory.GetFiles过载()。

You should use the Directory.GetFiles(path, searchPattern, SearchOption) overload of Directory.GetFiles().

path指定,是searchPattern指定你的通配符(例如,*,* .format)和SearchOption提供的选项包含子目录。

Path specifies the path, searchPattern specifies your wildcards (e.g., *, *.format) and SearchOption provides the option to include subdirectories.

此搜索返回数组的长度属性将提供适当的文件数为特定的搜索模式和选项:

The Length property of the return array of this search will provide the proper file count for your particular search pattern and option:

string[] files = directory.GetFiles(@"c:\windows\system32", "*.dll", SearchOption.AllDirectories);

return files.Length;

编辑:或者您可以使用的 Directory.EnumerateFiles方法

return Directory.EnumerateFiles(@"c:\windows\system32", "*.dll", SearchOption.AllDirectories).Count();

这篇关于查找文件数量具有特定扩展名,在所有子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆