如何使用C#DirectoryInfo枚举具有所有子目录的Files? [英] How to EnumerateFiles with all subdirectories with C# DirectoryInfo?

查看:341
本文介绍了如何使用C#DirectoryInfo枚举具有所有子目录的Files?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这段代码从DirectoryInfo中获取文件数组:

I found this code that gets an array of files out of a DirectoryInfo :

FileInfo[] fileInfoArray = di.EnumerateFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();

但它仅搜索DirectoryInfo路径的直接子代.即不包括孙子孙女.

But it only searches the direct children of the path of DirectoryInfo. i.e., it does not include grandchildren.

我想我需要在某个地方添加SearchOption.AllDirectories参数,但是在哪里?

I guess I need to add SearchOption.AllDirectories parameter to somewhere, but where?

我尝试过:

di.EnumerateFiles(SearchOption.AllDirectories).Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();

但是会产生错误.

那我该如何搜索包括所有子目录在内的模式?

So how do I search with a pattern, including all subdirectories ?

感谢您的帮助!

推荐答案

查看

Look at the overloads of DirectoryInfo.EnumerateFiles - there's no overload taking just a SearchOption, but you can give a string and a SearchOption:

var files = di.EnumerateFiles("*", SearchOption.AllDirectories)
              .Where(f => extensions.Contains(f.Extension.ToLower()))
              .ToArray();

这篇关于如何使用C#DirectoryInfo枚举具有所有子目录的Files?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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