GlobExtensions Class
Provides extension methods for working with glob patterns.
public static class GlobExtensionsMethods
| Name | Description |
|---|---|
| EnumerateFileInfos(IGlobEvaluatable, string, EnumerationOptions?) | Enumerates the files in the specified directory that match the glob pattern, as FileInfo objects. |
| EnumerateFileSystemEntries(IGlobEvaluatable, string, GlobPathFormat, EnumerationOptions?) | Enumerates file system entries (files and directories) in the specified directory that match the glob pattern. |
| EnumerateFileSystemEntries(IGlobEvaluatable, string, EnumerationOptions?) | Enumerates file system entries (files and directories) in the specified directory that match the glob pattern. |
| EnumerateFileSystemInfos(IGlobEvaluatable, string, EnumerationOptions?) | Enumerates the file system entries (files and directories) in the specified directory that match the glob pattern, as FileSystemInfo objects. |
| EnumerateFiles(IGlobEvaluatable, string, GlobPathFormat, EnumerationOptions?) | Enumerates files in the specified directory that match the glob pattern. |
| EnumerateFiles(IGlobEvaluatable, string, EnumerationOptions?) | Enumerates files in the specified directory that match the glob pattern. |
| IsMatch(IGlobEvaluatable, FileSystemEntry) | Determines whether the specified file system entry matches the glob pattern. |
| IsMatch(IGlobEvaluatable, ReadOnlySpan<char>) | Determines whether the specified path matches the glob pattern. |
| IsMatch(IGlobEvaluatable, ReadOnlySpan<char>, ReadOnlySpan<char>) | Determines whether the specified path matches the glob pattern. |
| IsMatch(IGlobEvaluatable, string) | Determines whether the specified path matches the glob pattern. |
| IsMatch(IGlobEvaluatable, string, string) | Determines whether the specified path matches the glob pattern. |
| IsPartialMatch(IGlobEvaluatable, FileSystemEntry) | Determines whether a file system entry directory should be recursed into when enumerating files. |
| IsPartialMatch(IGlobEvaluatable, ReadOnlySpan<char>) | Determines whether a directory should be recursed into when enumerating files. |
| IsPartialMatch(IGlobEvaluatable, string) | Determines whether a directory should be recursed into when enumerating files. |
| IsPartialMatch(IGlobEvaluatable, string, string) | Determines whether a directory should be recursed into when enumerating files. |
EnumerateFileInfos(IGlobEvaluatable, string, EnumerationOptions?)
GlobExtensions.csEnumerates the files in the specified directory that match the glob pattern, as FileInfo objects.
public static IEnumerable<FileInfo> EnumerateFileInfos(this IGlobEvaluatable glob, string directory, EnumerationOptions? options = null)Parameters
globIGlobEvaluatable- The glob pattern to match against.
directorystring- The directory to search.
optionsEnumerationOptions?- Options for controlling file enumeration behavior.
Returns
IEnumerable<FileInfo> An enumerable collection of the files that match the glob pattern.
Remarks
The objects are initialized with the data read during the enumeration, so reading their length or their timestamps does not access the file system again. See GlobExtensions.EnumerateFiles.
Example
using var rootDirectory = TemporaryDirectory.Create();
rootDirectory.CreateTextFile("logs/app.log", "started");
rootDirectory.CreateTextFile("logs/error.log", "");
// The FileInfo objects are filled during the enumeration: reading Length does not access the disk again
var glob = Glob.Parse("logs/*.log", GlobDialect.Standard);
var nonEmptyFiles = glob.EnumerateFileInfos(rootDirectory.FullPath)
.Where(file => file.Length > 0)
.Select(file => file.Name)
.ToList();
Assert.Equal(["app.log"], nonEmptyFiles);EnumerateFileSystemEntries overloads
EnumerateFileSystemEntries(IGlobEvaluatable, string, GlobPathFormat, EnumerationOptions?)
GlobExtensions.csEnumerates file system entries (files and directories) in the specified directory that match the glob pattern.
public static IEnumerable<string> EnumerateFileSystemEntries(this IGlobEvaluatable glob, string directory, GlobPathFormat pathFormat, EnumerationOptions? options = null)Parameters
globIGlobEvaluatable- The glob pattern to match against.
directorystring- The directory to search.
pathFormatGlobPathFormat- The format of the returned paths.
optionsEnumerationOptions?- Options for controlling file enumeration behavior.
Returns
IEnumerable<string> An enumerable collection of the paths of the entries that match the glob pattern.
Remarks
EnumerateFileSystemEntries(IGlobEvaluatable, string, EnumerationOptions?)
GlobExtensions.csEnumerates file system entries (files and directories) in the specified directory that match the glob pattern.
public static IEnumerable<string> EnumerateFileSystemEntries(this IGlobEvaluatable glob, string directory, EnumerationOptions? options = null)Parameters
globIGlobEvaluatable- The glob pattern to match against.
directorystring- The directory to search.
optionsEnumerationOptions?- Options for controlling file enumeration behavior.
Returns
IEnumerable<string> An enumerable collection of the full paths of the entries that match the glob pattern.
Remarks
EnumerateFileSystemInfos(IGlobEvaluatable, string, EnumerationOptions?)
GlobExtensions.csEnumerates the file system entries (files and directories) in the specified directory that match the glob pattern, as FileSystemInfo objects.
public static IEnumerable<FileSystemInfo> EnumerateFileSystemInfos(this IGlobEvaluatable glob, string directory, EnumerationOptions? options = null)Parameters
globIGlobEvaluatable- The glob pattern to match against.
directorystring- The directory to search.
optionsEnumerationOptions?- Options for controlling file enumeration behavior.
Returns
IEnumerable<FileSystemInfo> An enumerable collection of the entries that match the glob pattern: a FileInfo for a file, and a DirectoryInfo for a directory.
Remarks
EnumerateFiles overloads
EnumerateFiles(IGlobEvaluatable, string, GlobPathFormat, EnumerationOptions?)
GlobExtensions.csEnumerates files in the specified directory that match the glob pattern.
public static IEnumerable<string> EnumerateFiles(this IGlobEvaluatable glob, string directory, GlobPathFormat pathFormat, EnumerationOptions? options = null)Parameters
globIGlobEvaluatable- The glob pattern to match against.
directorystring- The directory to search.
pathFormatGlobPathFormat- The format of the returned paths.
optionsEnumerationOptions?- Options for controlling file enumeration behavior.
Returns
IEnumerable<string> An enumerable collection of the paths of the files that match the glob pattern.
Remarks
A relative path is relative to directory, so it starts with .. for a file found through a pattern that goes up from the directory. See GlobExtensions.EnumerateFiles.
Example
using var rootDirectory = TemporaryDirectory.Create();
rootDirectory.CreateEmptyFile("src/App/Program.cs");
rootDirectory.CreateEmptyFile("src/Lib/Helper.cs");
// Return the paths relative to the enumerated directory, as the pattern matches them
var glob = Glob.Parse("src/**/*.cs", GlobDialect.Standard);
var files = glob.EnumerateFiles(rootDirectory.FullPath, GlobPathFormat.RelativePath).Order(StringComparer.Ordinal).ToList();
Assert.Equal(
[
Path.Combine("src", "App", "Program.cs"),
Path.Combine("src", "Lib", "Helper.cs"),
],
files);EnumerateFiles(IGlobEvaluatable, string, EnumerationOptions?)
GlobExtensions.csEnumerates files in the specified directory that match the glob pattern.
public static IEnumerable<string> EnumerateFiles(this IGlobEvaluatable glob, string directory, EnumerationOptions? options = null)Parameters
globIGlobEvaluatable- The glob pattern to match against.
directorystring- The directory to search.
optionsEnumerationOptions?- Options for controlling file enumeration behavior.
Returns
IEnumerable<string> An enumerable collection of the full paths of the files that match the glob pattern.
Remarks
The enumeration starts at the base path of the pattern (see Glob.TrySplitBasePath), or at the base path of each include pattern of a GlobCollection. A pattern that starts with .. or that is absolute can therefore return files outside directory.
IsMatch overloads
IsMatch(IGlobEvaluatable, FileSystemEntry)
GlobExtensions.csDetermines whether the specified file system entry matches the glob pattern.
public static bool IsMatch(this IGlobEvaluatable glob, ref FileSystemEntry entry)Parameters
globIGlobEvaluatable- The glob pattern to match against.
entryFileSystemEntry- The file system entry to check.
Returns
bool true if the entry matches the pattern; otherwise, false.
IsMatch(IGlobEvaluatable, ReadOnlySpan<char>)
GlobExtensions.csDetermines whether the specified path matches the glob pattern.
public static bool IsMatch(this IGlobEvaluatable glob, ReadOnlySpan<char> path)Parameters
globIGlobEvaluatable- The glob pattern to match against.
pathReadOnlySpan<char>- The path to check.
Returns
bool true if the path matches the pattern; otherwise, false.
IsMatch(IGlobEvaluatable, ReadOnlySpan<char>, ReadOnlySpan<char>)
GlobExtensions.csDetermines whether the specified path matches the glob pattern.
public static bool IsMatch(this IGlobEvaluatable glob, ReadOnlySpan<char> directory, ReadOnlySpan<char> filename)Parameters
globIGlobEvaluatable- The glob pattern to match against.
directoryReadOnlySpan<char>- The directory part of the path to match.
filenameReadOnlySpan<char>- The filename part of the path to match.
Returns
bool true if the path matches the pattern; otherwise, false.
IsMatch(IGlobEvaluatable, string)
GlobExtensions.csDetermines whether the specified path matches the glob pattern.
public static bool IsMatch(this IGlobEvaluatable glob, string path)Parameters
globIGlobEvaluatable- The glob pattern to match against.
pathstring- The path to check.
Returns
bool true if the path matches the pattern; otherwise, false.
IsMatch(IGlobEvaluatable, string, string)
GlobExtensions.csDetermines whether the specified path matches the glob pattern.
public static bool IsMatch(this IGlobEvaluatable glob, string directory, string filename)Parameters
globIGlobEvaluatable- The glob pattern to match against.
directorystring- The directory part of the path to match.
filenamestring- The filename part of the path to match.
Returns
bool true if the path matches the pattern; otherwise, false.
IsPartialMatch overloads
IsPartialMatch(IGlobEvaluatable, FileSystemEntry)
GlobExtensions.csDetermines whether a file system entry directory should be recursed into when enumerating files.
public static bool IsPartialMatch(this IGlobEvaluatable glob, ref FileSystemEntry entry)Parameters
globIGlobEvaluatable- The glob pattern to check against.
entryFileSystemEntry- The file system entry to check.
Returns
bool true if the directory could contain matches; otherwise, false.
IsPartialMatch(IGlobEvaluatable, ReadOnlySpan<char>)
GlobExtensions.csDetermines whether a directory should be recursed into when enumerating files.
public static bool IsPartialMatch(this IGlobEvaluatable glob, ReadOnlySpan<char> folderPath)Parameters
globIGlobEvaluatable- The glob pattern to check against.
folderPathReadOnlySpan<char>- The folder path to check.
Returns
bool true if the directory could contain matches; otherwise, false.
IsPartialMatch(IGlobEvaluatable, string)
GlobExtensions.csDetermines whether a directory should be recursed into when enumerating files.
public static bool IsPartialMatch(this IGlobEvaluatable glob, string folderPath)Parameters
globIGlobEvaluatable- The glob pattern to check against.
folderPathstring- The folder path to check.
Returns
bool true if the directory could contain matches; otherwise, false.
IsPartialMatch(IGlobEvaluatable, string, string)
GlobExtensions.csDetermines whether a directory should be recursed into when enumerating files.
public static bool IsPartialMatch(this IGlobEvaluatable glob, string folderPath, string filename)Parameters
globIGlobEvaluatable- The glob pattern to check against.
folderPathstring- The folder path to check.
filenamestring- The filename part of the path to check.
Returns
bool true if the directory could contain matches; otherwise, false.