---
title: "Generate a unified diff patch in C#"
url: https://recursiveai.org/onediff/cookbook/export-unified-diff-patch/
description: "Generate a unified diff patch from two text versions in C#. Read both files, name the patch headers, and write a .patch file without modifying the inputs."
version: "1.0.1"
---

# Generate a unified diff patch 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/export-unified-diff-patch/export-unified-diff-patch.zip)

A unified patch lists changed lines and nearby unchanged lines.

```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;

string patch = Diff.Unified("off\n", "on\n");
Console.Write(patch);
```

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

Prints raw patch text:

```diff
--- a
+++ b
@@ -1 +1 @@
-off
+on
```

## The full program

The example reads two versions of a settings file, writes `settings.patch`, and leaves both inputs untouched. The zip includes `before.txt` and `after.txt`; both end with a newline.

`before.txt`:

```text
size=small
mode=auto
```

`after.txt`:

```text
size=large
mode=auto
```

```csharp
using OneDiff;

string before = File.ReadAllText("before.txt");
string after = File.ReadAllText("after.txt");
string patch = Diff.Unified(before, after, "a/settings.txt", "b/settings.txt");
if (patch.Length == 0)
{
    Console.WriteLine("No changes; no patch written.");
    return;
}

File.WriteAllText("settings.patch", patch);
Console.Write(patch);
Console.WriteLine("Wrote settings.patch");
```

```text
--- a/settings.txt
+++ b/settings.txt
@@ -1,2 +1,2 @@
-size=small
+size=large
 mode=auto
Wrote settings.patch
```

The last line is a status message, not part of the patch. If the input texts are identical, `Diff.Unified` returns an empty string and the program skips the write.

## Variations

**Export only the changes, with no unchanged lines.** Set `contextLines: 0`:

```csharp
using OneDiff;

Console.Write(Diff.Unified(
    "mode=auto\noff\n", "mode=auto\non\n",
    contextLines: 0));
```

```diff
--- a
+++ b
@@ -2 +2 @@
-off
+on
```

## Related recipes

- [Apply a unified diff patch](https://recursiveai.org/onediff/cookbook/apply-unified-patch/)
- [List changed files in a unified diff](https://recursiveai.org/onediff/cookbook/summarize-unified-patch-without-applying/)
- [Generate a Markdown text diff](https://recursiveai.org/onediff/cookbook/write-text-diff-as-markdown/)

<!-- article -->

## The short answer

Call `Diff.Unified(before, after)` to create the patch text that describes a change from the old version to the new one. The full program reads both files and saves the returned string directly with `File.WriteAllText`. It doesn’t apply the patch and doesn’t need a Git repository. If there are no changes, the return value is empty rather than a file header with no edits.

## How do you create a patch file from a diff?

Write the raw unified diff, not a Markdown code block or a terminal rendering. A unified patch begins with `---` and `+++` file headers, followed by one or more hunks. Each hunk records changes in one part of the file. In the full program’s patch, `@@ -1,2 +1,2 @@` says that the hunk starts at line 1 and covers two lines in both versions. `-` removes a line, `+` adds one, and a leading space preserves a context line. See the [GNU unified format description](https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html) and [OneDiff’s patch docs](https://recursiveai.org/onediff/docs/patches/#unified-diff).

The short example uses the default labels `a` and `b`. The full program instead passes `a/settings.txt` and `b/settings.txt` to describe two versions of the same target file. These labels go into the headers; they don’t tell `Diff.Unified` what to open. The actual input paths are the two arguments to `File.ReadAllText`. Save only the patch string, without the program’s confirmation line or any additional commentary.

## How many context lines should a unified diff have?

Start with the default three. These unchanged lines give the reader surrounding text and give a patch tool more information about where the change belongs. Changes separated by at most twice the chosen context length share a hunk. Set `contextLines: 0` when you want the shortest changed-line listing, as in the variation; it leaves out those matching cues.

OneDiff splits patch input at `\n` and retains line terminators. Don’t normalize them before comparing if a line-ending edit is part of the change you want to capture. A missing final newline isn’t silently corrected: a changed line without a terminator is followed by `\ No newline at end of file`. Keep that marker in the file you export.
