---
title: "List changed files in a unified diff in C#"
url: https://recursiveai.org/onediff/cookbook/summarize-unified-patch-without-applying/
description: "Parse a unified diff in C# and list file names and hunk counts without opening or changing the files named in the patch. Includes a sample text patch."
version: "1.0.1"
---

# List changed files in a unified diff in C#

**OneDiff** is a free, open-source diff library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDiff) · [GitHub](https://github.com/recursive-ai-oss/onediff-csharp) · [Docs](https://recursiveai.org/onediff/docs/) · [Download example (.zip)](https://recursiveai.org/onediff/cookbook/summarize-unified-patch-without-applying/summarize-unified-patch-without-applying.zip)

Copy `sample.diff` below or get it in the zip. Skips binary and rename-only sections.

```shell
dotnet add package RecursiveAI.OneDiff
```

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

```csharp
using OneDiff.Patching;
var text = File.ReadAllText("sample.diff");
var patch = UnifiedPatch.Parse(text);
foreach (var file in patch.Files)
    Console.WriteLine(
        $"{file.NewName}: {file.Hunks.Count}");
```

Unzip [the example](https://recursiveai.org/onediff/cookbook/summarize-unified-patch-without-applying/summarize-unified-patch-without-applying.zip), then `dotnet run`.

Prints each new-side name and its count of change blocks (hunks).

```text
b/notes.txt: 2
b/config.txt: 1
```

## The full program

The download contains this generated `sample.diff`. Put it next to your project to run the snippet at the top. The labels `a/notes.txt` and `b/notes.txt` are names in the patch, not files you need to create.

```diff
--- a/notes.txt
+++ b/notes.txt
@@ -1 +1 @@
-title: draft
+title: final
@@ -3 +3 @@
-status: open
+status: closed
--- a/config.txt
+++ b/config.txt
@@ -1 +1 @@
-debug=true
+debug=false
```

```csharp
using OneDiff.Patching;

var text = File.ReadAllText("sample.diff");
if (!UnifiedPatch.TryParse(text, out var patch, out var error))
{
    Console.Error.WriteLine($"Line {error.Line}: {error.Kind}: {error.Message}");
    return 1;
}

foreach (var file in patch.Files)
    Console.WriteLine($"{file.OldName} -> {file.NewName}: hunks: {file.Hunks.Count}");

return 0;
```

`dotnet run` prints:

```text
a/notes.txt -> b/notes.txt: hunks: 2
a/config.txt -> b/config.txt: hunks: 1
```

The only file the example reads is `sample.diff`. `Parse` throws on malformed patch text. The full program uses `TryParse` instead: if parsing fails, it reports the error kind and line and exits with code `1` before printing a summary.

## Variations

**Count added and removed lines.** Replace the `foreach` in the full program to count lines inside the parsed hunks, ignoring unchanged context:

```csharp
foreach (var file in patch.Files)
{
    int added = 0, removed = 0;
    foreach (var hunk in file.Hunks)
        foreach (var line in hunk.Lines)
        {
            if (line.Kind == UnifiedLineKind.Insert) added++;
            if (line.Kind == UnifiedLineKind.Delete) removed++;
        }
    Console.WriteLine($"{file.NewName}: +{added} -{removed}");
}
```

```text
b/notes.txt: +2 -2
b/config.txt: +1 -1
```

## Related recipes

- [Apply a unified patch to a string](https://recursiveai.org/onediff/cookbook/apply-unified-patch/)
- [Apply a multi-file unified patch](https://recursiveai.org/onediff/cookbook/apply-multifile-unified-patch-safely/)
- [Write a line diff as a Markdown report](https://recursiveai.org/onediff/cookbook/write-line-diff-markdown-report/)

## The short answer

Read the patch text and call `UnifiedPatch.TryParse`. On success, loop over `patch.Files` and read each entry’s `OldName`, `NewName`, and `Hunks.Count`. That gives you a summary of the parsed text sections even when the files they describe aren’t on disk. On failure, `PatchParseError` tells you the kind, line, offset, and message. Don’t interpret success as validation of a complete Git patch or confirmation that the patch applies.

## What is a hunk in a unified diff?

A hunk is a block of changes in one file, optionally including nearby unchanged lines for context. The [GNU unified format](https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html) uses `---` and `+++` for the file names and an `@@` header for each hunk, with the old range after `-` and the new range after `+`. In the sample, `@@ -3 +3 @@` describes a change at line 3 on each side. The count is omitted because each range is one line long. The `notes.txt` section has two separate headers, so the summary reports two hunks. The count isn’t a count of changed lines: one hunk can contain several additions and deletions plus context.

## What’s the difference between reading a diff and applying it?

Reading a diff tells you what the patch text describes. Applying it also requires the original content and checks the hunk lines against that content. The example never performs that second step. You can use the summary to show which names a text patch mentions before anyone decides to apply it. Treat the names as labels from the headers: the `b/` in `b/notes.txt` isn’t removed by the example and doesn’t tell you where a file is on disk.

## Does a unified diff list binary changes and renames?

Git can report binary changes and rename information, but [OneDiff’s unified parser](https://recursiveai.org/onediff/docs/patches/) skips binary and rename-only sections. It can retain extended header lines that precede a parsed text file section, but those aren’t a separate list of all changes in the patch. An empty `Files` list means there were no file sections to report; it does not mean the patch described no changes. This summary is for unified text hunks, not a replacement for a Git change inventory.
