GlobExtensions Class

Provides extension methods for working with glob patterns.

public static class GlobExtensions

Methods

NameDescription
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.cs

Enumerates 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

glob IGlobEvaluatable
The glob pattern to match against.
directory string
The directory to search.
options EnumerationOptions?
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.cs

Enumerates 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
glob IGlobEvaluatable
The glob pattern to match against.
directory string
The directory to search.
pathFormat GlobPathFormat
The format of the returned paths.
options EnumerationOptions?
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.cs

Enumerates 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
glob IGlobEvaluatable
The glob pattern to match against.
directory string
The directory to search.
options EnumerationOptions?
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.cs

Enumerates 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

glob IGlobEvaluatable
The glob pattern to match against.
directory string
The directory to search.
options EnumerationOptions?
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.cs

Enumerates 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
glob IGlobEvaluatable
The glob pattern to match against.
directory string
The directory to search.
pathFormat GlobPathFormat
The format of the returned paths.
options EnumerationOptions?
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.cs

Enumerates files in the specified directory that match the glob pattern.

public static IEnumerable<string> EnumerateFiles(this IGlobEvaluatable glob, string directory, EnumerationOptions? options = null)
Parameters
glob IGlobEvaluatable
The glob pattern to match against.
directory string
The directory to search.
options EnumerationOptions?
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.cs

Determines whether the specified file system entry matches the glob pattern.

public static bool IsMatch(this IGlobEvaluatable glob, ref FileSystemEntry entry)
Parameters
glob IGlobEvaluatable
The glob pattern to match against.
entry FileSystemEntry
The file system entry to check.
Returns

bool true if the entry matches the pattern; otherwise, false.

IsMatch(IGlobEvaluatable, ReadOnlySpan<char>)

GlobExtensions.cs

Determines whether the specified path matches the glob pattern.

public static bool IsMatch(this IGlobEvaluatable glob, ReadOnlySpan<char> path)
Parameters
glob IGlobEvaluatable
The glob pattern to match against.
path ReadOnlySpan<char>
The path to check.
Returns

bool true if the path matches the pattern; otherwise, false.

IsMatch(IGlobEvaluatable, ReadOnlySpan<char>, ReadOnlySpan<char>)

GlobExtensions.cs

Determines whether the specified path matches the glob pattern.

public static bool IsMatch(this IGlobEvaluatable glob, ReadOnlySpan<char> directory, ReadOnlySpan<char> filename)
Parameters
glob IGlobEvaluatable
The glob pattern to match against.
directory ReadOnlySpan<char>
The directory part of the path to match.
filename ReadOnlySpan<char>
The filename part of the path to match.
Returns

bool true if the path matches the pattern; otherwise, false.

IsMatch(IGlobEvaluatable, string)

GlobExtensions.cs

Determines whether the specified path matches the glob pattern.

public static bool IsMatch(this IGlobEvaluatable glob, string path)
Parameters
glob IGlobEvaluatable
The glob pattern to match against.
path string
The path to check.
Returns

bool true if the path matches the pattern; otherwise, false.

IsMatch(IGlobEvaluatable, string, string)

GlobExtensions.cs

Determines whether the specified path matches the glob pattern.

public static bool IsMatch(this IGlobEvaluatable glob, string directory, string filename)
Parameters
glob IGlobEvaluatable
The glob pattern to match against.
directory string
The directory part of the path to match.
filename string
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.cs

Determines whether a file system entry directory should be recursed into when enumerating files.

public static bool IsPartialMatch(this IGlobEvaluatable glob, ref FileSystemEntry entry)
Parameters
glob IGlobEvaluatable
The glob pattern to check against.
entry FileSystemEntry
The file system entry to check.
Returns

bool true if the directory could contain matches; otherwise, false.

IsPartialMatch(IGlobEvaluatable, ReadOnlySpan<char>)

GlobExtensions.cs

Determines whether a directory should be recursed into when enumerating files.

public static bool IsPartialMatch(this IGlobEvaluatable glob, ReadOnlySpan<char> folderPath)
Parameters
glob IGlobEvaluatable
The glob pattern to check against.
folderPath ReadOnlySpan<char>
The folder path to check.
Returns

bool true if the directory could contain matches; otherwise, false.

IsPartialMatch(IGlobEvaluatable, string)

GlobExtensions.cs

Determines whether a directory should be recursed into when enumerating files.

public static bool IsPartialMatch(this IGlobEvaluatable glob, string folderPath)
Parameters
glob IGlobEvaluatable
The glob pattern to check against.
folderPath string
The folder path to check.
Returns

bool true if the directory could contain matches; otherwise, false.

IsPartialMatch(IGlobEvaluatable, string, string)

GlobExtensions.cs

Determines whether a directory should be recursed into when enumerating files.

public static bool IsPartialMatch(this IGlobEvaluatable glob, string folderPath, string filename)
Parameters
glob IGlobEvaluatable
The glob pattern to check against.
folderPath string
The folder path to check.
filename string
The filename part of the path to check.
Returns

bool true if the directory could contain matches; otherwise, false.