---
title: "Copy and paste system clipboard text in cross-platform C#"
url: https://recursiveai.org/oneplatform/cookbook/read-write-system-clipboard-text/
description: "Read and write Unicode text on the system clipboard in a C# desktop app. Keep the event loop running so other apps can paste your text."
version: "1.0.0"
---

# Copy and paste system clipboard text in cross-platform C#

**OnePlatform** is a free, open-source library for windows, input, clipboard, displays and gamepads in .NET from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePlatform) · [GitHub](https://github.com/recursive-ai-oss/oneplatform-csharp) · [Docs](https://recursiveai.org/oneplatform/docs/) · [Download example (.zip)](https://recursiveai.org/oneplatform/cookbook/read-write-system-clipboard-text/read-write-system-clipboard-text.zip)

Read and write clipboard text on the thread that runs your desktop app; this API doesn’t handle images or files. OnePlatform supports the system clipboard on Windows, macOS, and Linux (X11 and Wayland); the snippet works unchanged on Windows, macOS, and X11, while Wayland needs a key press or click.

```shell
dotnet add package RecursiveAI.OnePlatform
```

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

```csharp
using OnePlatform;

using var app = PlatformApp.Create();
var clipboard = app.Clipboard;
if (!clipboard.SetText("Café 世界"))
    throw new InvalidOperationException("Copy failed.");
Console.WriteLine(clipboard.GetText() ?? "No text");
```

[The example](https://recursiveai.org/oneplatform/cookbook/read-write-system-clipboard-text/read-write-system-clipboard-text.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

The snippet reads back its own write and then exits. Use the download to paste between apps or to copy on Wayland in response to a key press. On X11, the snippet printed:

```text
Café 世界
```

## Full program

```csharp
using OnePlatform;

using var app = PlatformApp.Create();
if (app.Backend == PlatformBackendKind.Headless)
    throw new InvalidOperationException("No desktop session found.");

app.CreateWindow(new() { Title = "Clipboard demo" });
Console.WriteLine("Focus the window. C copies; V pastes.");
while (true)
{
    app.PumpEvents(100);
    while (app.TryGetEvent(out var evt))
    {
        if (evt.Kind is PlatformEventKind.WindowCloseRequested
            or PlatformEventKind.QuitRequested)
            return;
        if (evt.Kind != PlatformEventKind.KeyDown || evt.IsRepeat)
            continue;
        var clipboard = app.Clipboard;
        if (evt.Key == Key.C)
            Console.WriteLine(clipboard.SetText("Café 世界")
                ? "Copied" : "Copy failed");
        if (evt.Key == Key.V)
            Console.WriteLine(clipboard.GetText() ?? "No text");
    }
}
```

Focus **Clipboard demo**, press **C** to copy `Café 世界`, and switch to another app to paste it while the demo stays open. Copy `Déjà vu` from that other app, return to **Clipboard demo**, and press **V** to print what you copied. Close the window to quit.

```text
Focus the window. C copies; V pastes.
Copied
Déjà vu
```

If `SetText` returns `false`, the write didn’t succeed. `GetText` returns `null` for absent, non-text, or oversized content, or when the clipboard owner doesn’t answer in time; the example prints `No text`.

## Variations

On X11, read highlighted text (the middle-click selection) instead of the ordinary copy/paste clipboard. Replace `clipboard.GetText()` in the paste handler with:

```csharp
clipboard.GetText(ClipboardSelection.Primary)
```

## Related recipes

- [Create a native desktop window in C#](https://recursiveai.org/oneplatform/cookbook/create-native-cross-platform-window/)
- [Simulate mouse and keyboard input in headless tests](https://recursiveai.org/oneplatform/cookbook/test-window-input-headlessly/)

## The short answer

For cross-platform clipboard text in C#, call `app.Clipboard.SetText(text)` to copy and `app.Clipboard.GetText()` to paste. Call both synchronously on the thread that runs `PlatformApp`. Keep pumping the desktop event loop after copying so the backend can serve paste requests. A headless app has a private clipboard, so a successful read in a headless test doesn’t mean text reached the system clipboard.

## Why can’t another app paste the text I copied?

On X11 the source keeps ownership of the selection and serves the text when another app asks for it. If the owner quits or stops pumping events, don’t count on the text remaining available. A clipboard manager can affect what persists. The snippet at the top is only a quick read-back; the full program leaves the owning app running.

On Wayland, copy only in response to a key press or click in the focused window, not at startup. Some Wayland desktops don’t offer clipboard access to the app; check `app.Clipboard.IsAvailable` before offering clipboard actions, and still check the result of a write.

## What happens when the clipboard doesn’t contain text?

`GetText` returns `null`; it doesn’t turn an image, file list, or other non-text payload into a string. Treat `null` differently from an empty string. The API is for UTF-8 text, with a limit of 4 MiB measured in UTF-8 bytes, not the C# string’s character count. When another program owns the clipboard, a synchronous read may wait for that owner to answer, so read in response to a paste rather than polling every frame.

Use the default selection for normal copy and paste. Use `ClipboardSelection.Primary` only when integrating with X11’s separate selection for highlighted text. Reading the primary selection doesn’t tell you what the user last copied into the ordinary clipboard.
