Skip to content

Instantly share code, notes, and snippets.

@JeremyKuhne
Last active March 10, 2018 04:15
Show Gist options
  • Save JeremyKuhne/fad13691931258e5ca798baac5bd5c1c to your computer and use it in GitHub Desktop.
Save JeremyKuhne/fad13691931258e5ca798baac5bd5c1c to your computer and use it in GitHub Desktop.
Sample to get files with given extensions
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