RobotsFile Class

Represents a parsed robots.txt file.

public sealed class RobotsFile

Remarks

The parser is lenient: unknown directives and malformed lines are skipped rather than throwing, matching the behaviour of most web crawlers. Each skipped line is recorded in RobotsFile.ParseErrors so callers can audit file quality.

Supported directives: User-agent, Allow, Disallow, Crawl-delay, and Sitemap.

Path patterns in Allow and Disallow directives support Google's wildcard extensions: * matches any sequence of characters, and a trailing $ anchors the pattern to the end of the path.

Example

var robots = RobotsFile.Parse("""
    User-agent: *
    Disallow: /private/
    Allow: /public/

    Sitemap: https://example.com/sitemap.xml
    """);

Assert.True(robots.IsAllowed("Googlebot", "/public/page"));
Assert.False(robots.IsAllowed("Googlebot", "/private/page"));
Assert.Equal(["https://example.com/sitemap.xml"], robots.Sitemaps);

// Invalid lines are reported instead of throwing
Assert.Empty(robots.ParseErrors);

Properties

NameDescription
GroupsGets the groups of directives defined in the file, in the order they appear.
ParseErrorsGets the non-fatal parse errors encountered while reading the file.
SitemapsGets the sitemap URLs declared by Sitemap: directives, in the order they appear.

Gets the groups of directives defined in the file, in the order they appear.

public IReadOnlyList<RobotsGroup> Groups { get; }

ParseErrors

RobotsFile.cs

Gets the non-fatal parse errors encountered while reading the file.

public IReadOnlyList<RobotsParseError> ParseErrors { get; }

Property value

IReadOnlyList<RobotsParseError>

Remarks

The list is empty when the file is fully valid. Each entry records the 1-based RobotsParseError.LineNumber, the raw RobotsParseError.Line text, and the RobotsParseError.Kind of problem.

Gets the sitemap URLs declared by Sitemap: directives, in the order they appear.

public IReadOnlyList<string> Sitemaps { get; }

Property value

IReadOnlyList<string>

Methods

NameDescription
GetCrawlDelay(string)Returns the crawl delay for the given userAgent, or null when none is specified.
GetGroup(string)Returns the RobotsGroup that best matches the given userAgent.
IsAllowed(string, string)Determines whether the given userAgent is allowed to access path.
IsAllowed(string, Uri)Determines whether the given userAgent is allowed to access the URL represented by uri.
Parse(ReadOnlySpan<char>)Parses a robots.txt file from a ReadOnlySpan<T> of characters.
Parse(string)Parses a robots.txt file from a string.
ParseAsync(Stream, Encoding?, CancellationToken)Asynchronously parses a robots.txt file from a Stream.
ParseAsync(TextReader, CancellationToken)Asynchronously parses a robots.txt file from a TextReader.

GetCrawlDelay(string)

RobotsFile.cs

Returns the crawl delay for the given userAgent, or null when none is specified.

public TimeSpan? GetCrawlDelay(string userAgent)

Parameters

userAgent string

Returns

TimeSpan?

GetGroup(string)

RobotsFile.cs

Returns the RobotsGroup that best matches the given userAgent.

public RobotsGroup? GetGroup(string userAgent)

Parameters

userAgent string

Returns

RobotsGroup?

Remarks

An exact (case-insensitive) match takes precedence over a catch-all (*) group. Returns null when no group matches.

When several groups declare the selected user-agent token, their rules are merged into a single group, as required by RFC 9309 section 2.2.1. The merged group is not one of the instances in RobotsFile.Groups: it lists the union of the declared tokens, the rules of every contributing group in file order, and the first Crawl-delay that was specified. When exactly one group matches, that instance is returned as-is.

IsAllowed overloads

IsAllowed(string, string)

RobotsFile.cs

Determines whether the given userAgent is allowed to access path.

public bool IsAllowed(string userAgent, string path)
Parameters
userAgent string
The name of the crawling agent (e.g. "Googlebot").
path string
The URL path (and optional query string) to check.
Returns

bool

Remarks

When no group matches the user-agent, access is allowed by default.

IsAllowed(string, Uri)

RobotsFile.cs

Determines whether the given userAgent is allowed to access the URL represented by uri.

public bool IsAllowed(string userAgent, Uri uri)
Parameters
userAgent string
uri Uri
Returns

bool

Remarks

Only the path and query components of uri are evaluated against the rules. When no group matches the user-agent, access is allowed by default.

Parse overloads

Parse(ReadOnlySpan<char>)

RobotsFile.cs

Parses a robots.txt file from a ReadOnlySpan<T> of characters.

public static RobotsFile Parse(ReadOnlySpan<char> content)
Parameters
content ReadOnlySpan<char>
Returns

RobotsFile

Parse(string)

RobotsFile.cs

Parses a robots.txt file from a string.

public static RobotsFile Parse(string content)
Parameters
content string
The full text content of the robots.txt file.
Returns

RobotsFile

ParseAsync overloads

ParseAsync(Stream, Encoding?, CancellationToken)

RobotsFile.cs

Asynchronously parses a robots.txt file from a Stream.

net10.0

public static Task<RobotsFile> ParseAsync(Stream stream, Encoding? encoding = null, CancellationToken cancellationToken = default)

net11.0

public static Task<RobotsFile> ParseAsync(Stream stream, Encoding? encoding = null, CancellationToken cancellationToken = default)
Parameters
stream Stream
The stream to read from.
encoding Encoding?
The text encoding to use. Defaults to Encoding.UTF8 when null.
cancellationToken CancellationToken
A token to cancel the operation.
Returns

Task<RobotsFile>

ParseAsync(TextReader, CancellationToken)

RobotsFile.cs

Asynchronously parses a robots.txt file from a TextReader.

net10.0

public static Task<RobotsFile> ParseAsync(TextReader reader, CancellationToken cancellationToken = default)

net11.0

public static Task<RobotsFile> ParseAsync(TextReader reader, CancellationToken cancellationToken = default)
Parameters
reader TextReader
The text reader to read from.
cancellationToken CancellationToken
A token to cancel the operation.
Returns

Task<RobotsFile>