我可以理解为什么你发现很难弄清楚这一点 - 周围没有太多信息,所以我最终挖掘了源代码来解决问题。对于以下示例,我使用以下目录结构:
D:\Root\
|- Child\
| |- Grandchild\
| | |- Grandchild.jpg
| | |- Grandchild.txt
| |- Child.html
| |- Child.txt
|- Root.txt
Run Code Online (Sandbox Code Playgroud)
并导入以下命名空间:
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Enumeration;
Run Code Online (Sandbox Code Playgroud)
var enumeration = new FileSystemEnumerable<FileSystemInfo>(
@"D:\Root",
// What's returned here has to match the generic type
// passed in when creating the FileSystemEnumerable.
// As DirectoryInfo inherits from FileSystemInfo, we
// can use FileSystemInfo for both directories and files.
(ref FileSystemEntry entry) => entry.ToFileSystemInfo(),
// Should be self-explanatory.
new EnumerationOptions() { RecurseSubdirectories = true })
{
// We want to include everything: all directories and files.
ShouldIncludePredicate = (ref FileSystemEntry entry) => true
};
Run Code Online (Sandbox Code Playgroud)
作为FileSystemEnumerable<T>Implements IEnumerable<T>,对于我们的示例来说,这是一个迭代集合的情况FileSystemInfo- 这里没有什么特别的:
foreach (var item in enumeration)
{
Console.WriteLine($"{item.FullName} - {item.LastAccessTime}");
}
Run Code Online (Sandbox Code Playgroud)
打印:
D:\Root\Child - 03/10/2020 18:34:16
D:\Root\Root.txt - 03/10/2020 17:39:54
D:\Root\Child\Child.html - 03/10/2020 18:27:20
D:\Root\Child\Child.txt - 03/10/2020 17:40:15
D:\Root\Child\Grandchild - 03/10/2020 18:44:36
D:\Root\Child\Grandchild\Grandchild.jpg - 03/10/2020 18:44:28
D:\Root\Child\Grandchild\Grandchild.txt - 03/10/2020 17:40:10
Run Code Online (Sandbox Code Playgroud)
你会注意到几件事:
// We're returning strings this time.
var enumeration = new FileSystemEnumerable<string>(
@"D:\Root",
// Returns the full path to the directory.
(ref FileSystemEntry entry) => entry.ToFullPath(),
new EnumerationOptions() { RecurseSubdirectories = true })
{
// Only include directories in the result set.
ShouldIncludePredicate = (ref FileSystemEntry entry) => entry.IsDirectory
};
Run Code Online (Sandbox Code Playgroud)
运行这个:
foreach (var item in enumeration)
{
Console.WriteLine(item);
}
Run Code Online (Sandbox Code Playgroud)
印刷:
D:\Root\Child
D:\Root\Child\Grandchild
Run Code Online (Sandbox Code Playgroud)
var extensions = new List<string> { ".html", ".jpg" };
// Back to using FileSystemInfo.
var enumeration = new FileSystemEnumerable<FileSystemInfo>(
@"D:\Root",
(ref FileSystemEntry entry) => entry.ToFileSystemInfo(),
new EnumerationOptions() { RecurseSubdirectories = true })
{
ShouldIncludePredicate = (ref FileSystemEntry entry) =>
{
// Skip directories.
if (entry.IsDirectory)
{
return false;
}
foreach (string extension in extensions)
{
var fileExtension = Path.GetExtension(entry.FileName);
if (fileExtension.EndsWith(extension, StringComparison.OrdinalIgnoreCase))
{
// Include the file if it matches one of our extensions.
return true;
}
}
// Doesn't match, so exclude it.
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
运行这个:
foreach (var item in enumeration)
{
Console.WriteLine(item.FullName);
}
Run Code Online (Sandbox Code Playgroud)
印刷:
D:\Root\Child\Child.html
D:\Root\Child\Grandchild\Grandchild.jpg
Run Code Online (Sandbox Code Playgroud)
我刚刚找到了另一个委托,它控制递归行为。假设我们修改原始目录结构以包含一个新目录,其中包含我们要忽略的文件:
D:\Root\
|- Child\
| |- Grandchild\
| | |- Grandchild.jpg
| | |- Grandchild.txt
| |- GrandchildIgnore\
| | |- GrandchildIgnore.txt
| |- Child.html
| |- Child.txt
|- Root.txt
Run Code Online (Sandbox Code Playgroud)
要获取所有其他文件的列表,同时忽略新目录中的文件,我们可以设置ShouldRecursePredicate:
var enumeration = new FileSystemEnumerable<FileSystemInfo>(
@"D:\Root",
(ref FileSystemEntry entry) => entry.ToFileSystemInfo(),
new EnumerationOptions() { RecurseSubdirectories = true })
{
ShouldIncludePredicate = (ref FileSystemEntry entry) => !entry.IsDirectory,
// If the directory's name ends with ignore, skip it.
ShouldRecursePredicate = (ref FileSystemEntry entry) => !entry.ToFullPath().EndsWith("Ignore", StringComparison.OrdinalIgnoreCase))
};
Run Code Online (Sandbox Code Playgroud)
运行这个:
foreach (var item in enumeration)
{
Console.WriteLine(item.FullName);
}
Run Code Online (Sandbox Code Playgroud)
印刷:
D:\Root\Root.txt
D:\Root\Child\Child.html
D:\Root\Child\Child.txt
D:\Root\Child\Grandchild\Grandchild.jpg
D:\Root\Child\Grandchild\Grandchild.txt
Run Code Online (Sandbox Code Playgroud)
如我们所愿,排除目录。
var enumeration = new FileSystemEnumerable<(long, string)>(
@"D:\Root",
(ref FileSystemEntry entry) => (entry.Length, entry.FileName.ToString()))
{
ShouldIncludePredicate = (ref FileSystemEntry entry) => !entry.IsDirectory
};
Run Code Online (Sandbox Code Playgroud)
给定 1 个文件,其中D:\Root有 122 字节,运行以下命令:
foreach (var (length, fileName) in enumeration)
{
Console.WriteLine($"Name: {fileName} Length: {length}");
}
Run Code Online (Sandbox Code Playgroud)
印刷:
Name: Root.txt Length: 122
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2004 次 |
| 最近记录: |