Last active
March 10, 2018 04:15
-
-
Save JeremyKuhne/fad13691931258e5ca798baac5bd5c1c to your computer and use it in GitHub Desktop.
Sample to get files with given extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IEnumerable<FileInfo> GetFilesWithExtensions(string directory, | |
bool recursive, params string[] extensions) | |
{ | |
return new FileSystemEnumerable<FileInfo>( | |
directory, | |
(ref FileSystemEntry entry) => (FileInfo)entry.ToFileSystemInfo(), | |
new EnumerationOptions() { RecurseSubdirectories = recursive }) | |
{ | |
ShouldIncludePredicate = (ref FileSystemEntry entry) => | |
{ | |
if (entry.IsDirectory) return false; | |
foreach (string extension in extensions) | |
{ | |
// This line is using multiple new span APIs- there are no allocations involved | |
if (Path.GetExtension(entry.FileName).SequenceEqual(extension)) | |
return true; | |
} | |
return false; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment