简体   繁体   English

是否可以在DirectoryInfo.GetFiles上启用收益回报?

[英]Is it possible to enable yield return on DirectoryInfo.GetFiles?

I'm calling GetFiles and I must return the files found in all nested directories. 我正在调用GetFiles ,我必须返回在所有嵌套目录中找到的文件。 I'd like to be able to do something with the found files as they get returned. 我希望能够在找到的文件返回时对其进行处理。 Is it possible to call GetFiles in a way to allow yield return? 是否可以以允许收益率的方式调用GetFiles? Or is it necessary to roll my own version of GetFiles that uses yield return. 还是有必要推出我自己的使用yield return的GetFiles版本。

I was thinking of something like DirectoryInfo("MyDir").GetFiles("*.txt",SearchOptions.All).ForEach(dostuff) 我在想类似DirectoryInfo("MyDir").GetFiles("*.txt",SearchOptions.All).ForEach(dostuff)

where dostuff is a delegate dostuff是代表

Yes, DirectoryInfo.EnumerateFiles returns a lazy-loaded IEnumerable 是的, DirectoryInfo.EnumerateFiles返回一个延迟加载的IEnumerable

Documentation 文献资料

Your line would be: 您的行将是:

(new DirectoryInfo("MyDir")).EnumerateFiles("*.txt",SearchOptions.All).ForEach(dostuff)

Just use DirectoryInfo("MyDir").EnumerateFiles() , it already does a yield return for you. 只需使用DirectoryInfo("MyDir").EnumerateFiles() ,它已经为您带来了yield return

It is available from .NET 4 and returns an IEnumerable<FileInfo> . 它可以从.NET 4获得,并返回IEnumerable<FileInfo>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM