---
title: "Detect an invalid replay file in C#"
url: https://recursiveai.org/onescope/cookbook/detect-invalid-replay-recording/
description: "Catch OneScope’s ScopeFormatException when your replay parser rejects a local recording, and report the reason without confusing it with a file read error."
version: "1.0.0"
---

# Detect an invalid replay file in C#

**OneScope** is a free, open-source viewer for simulation recordings, with a desktop window or command-line control, from Recursive AI. MIT or Apache-2.0.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneScope.Sdk) · [GitHub](https://github.com/recursive-ai-oss/onescope-csharp) · [Docs](https://recursiveai.org/onescope/docs/) · [Download example (.zip)](https://recursiveai.org/onescope/cookbook/detect-invalid-replay-recording/detect-invalid-replay-recording.zip)

A replay file here is a recording in a OneScope module’s format, and that module decides which bytes it accepts. If you have your own format, see [Write a custom replay viewer module](https://recursiveai.org/onescope/cookbook/write-a-onescope-module/).

Use `CounterModule.cs` and `broken.replay` from the zip.

```shell
dotnet add package RecursiveAI.OneScope.Sdk
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneScope.Sdk;
try { CounterModule.OpenFile("broken.replay"); }
catch (ScopeFormatException error)
{
    Console.WriteLine(error.Message);
}
```

Unzip [the example](https://recursiveai.org/onescope/cookbook/detect-invalid-replay-recording/detect-invalid-replay-recording.zip), then `dotnet run`.

```text
broken.replay: Expected 'COUNT ' at offset 0.
```

## Parser and inputs

`broken.replay` contains `COUNT` followed by a newline. The parser expects `COUNT ` (including the space), then a step count. The good file, `counter.replay`, contains:

```text
COUNT 4
```

`OpenFile` is a helper in `CounterModule.cs` that reads the file and calls `Open`; the full program calls `Open` directly.

The program reports the broken header, then opens the good file with the same parser:

```csharp
using OneScope.Sdk;

const string path = "broken.replay";
var bytes = File.ReadAllBytes(path);
try
{
    new CounterModule().Open(bytes, path);
    Console.WriteLine("Header accepted.");
}
catch (ScopeFormatException error)
{
    Console.WriteLine(error.Message);
}

const string good = "counter.replay";
var source = new CounterModule().Open(File.ReadAllBytes(good), good);
Console.WriteLine($"{good}: {source.Recording.StepCount} steps");
```

```text
broken.replay: Expected 'COUNT ' at offset 0.
counter.replay: 4 steps
```

A missing file is a read error from `File.ReadAllBytes`, not a `ScopeFormatException` from the parser.

## Variations

To test a recognized header with a bad count, replace the call inside `try` in `Program.cs`:

```csharp
new CounterModule().Open("COUNT -1"u8.ToArray(), "bad-count.replay");
```

```text
bad-count.replay: Expected 0 to 1000 steps after 'COUNT '.
```

## Related recipes

- [Write a custom replay viewer module](https://recursiveai.org/onescope/cookbook/write-a-onescope-module/)
- [Seek to a specific tick](https://recursiveai.org/onescope/cookbook/seek-simulation-replay-to-step/)

## The short answer

Pass the replay bytes and the file name to the matching module’s `Open` method. With a module that follows the [OneScope authoring contract](https://recursiveai.org/onescope/docs/module-authoring/), catch `ScopeFormatException` to report that the module rejected the data. The exception’s `Input` property identifies what the parser was reading, and `Message` includes the parser’s reason. OneScope defines the exception; the module decides which bytes it accepts.

## How do you check a file’s format from its header in C#?

Check the contents, not the `.replay` extension. The counter example starts with a fixed byte prefix, `COUNT `; a replay format can use a fixed identifier like this to distinguish its files from other formats. The example then checks the step count before allocating the array of inputs. A file with the right prefix but a negative count still fails, as the variation shows. In a binary format with variable-length records, check that each declared length fits in the bytes that remain before slicing or allocating. Include the offending field or byte offset in the exception so the reader knows what to fix.

A rejection means the selected module cannot open these bytes. It does not distinguish a damaged replay from a valid replay for a different module. If you work with several formats, use the module for the recording you expect; a name or extension is not a substitute for parsing. Also decide whether you want to check just whether a file opens, or whether it replays as recorded. With recorded state hashes, `History.Scan()` can expose a playback mismatch through `FirstHashMismatch`. That check happens during playback and is separate from a parser rejection.
