Clipboard
Hermes provides a simple static API for reading and writing text to the system clipboard. It works across all platforms with no external dependencies — except Linux, which requires xclip.
Quick Reference
using Hermes;
// Copy text to clipboard
Clipboard.SetText("Hello from Hermes!");
// Read text from clipboard
string? text = Clipboard.GetText();
// Convenience wrappers on HermesApplication
HermesApplication.SetClipboardText("Hello!");
string? current = HermesApplication.GetClipboardText();Setting Text
Clipboard.SetText(string text) copies the given string to the system clipboard.
Clipboard.SetText("Some text to copy");The method validates its input — passing null, an empty string, or whitespace throws an ArgumentException. This prevents accidentally clearing the clipboard with blank content.
Getting Text
Clipboard.GetText() returns the current clipboard contents as a string?. If the clipboard is empty or doesn't contain text, it returns null.
var text = Clipboard.GetText();
if (text is not null)
{
Console.WriteLine($"Clipboard contains: {text}");
}Reading the clipboard never modifies its contents.
Platform Details
| Platform | Mechanism | Notes |
|---|---|---|
| macOS | pbcopy / pbpaste | Ships with macOS, no dependencies |
| Windows | Win32 Clipboard API | Direct API calls, fastest performance |
| Linux | xclip | Must be installed separately |
Linux requires xclip
On Linux, clipboard operations use xclip. If it isn't installed, Hermes throws a PlatformNotSupportedException with a message prompting you to install it:
sudo apt install xclipError Handling
| Exception | Cause |
|---|---|
ArgumentException | SetText called with null, empty, or whitespace text |
PlatformNotSupportedException | Unsupported platform, or xclip missing on Linux |
Blazor Example
A simple copy-to-clipboard button in a Blazor component:
@using Hermes
<button @onclick="CopyToClipboard">Copy Link</button>
<button @onclick="PasteFromClipboard">Paste</button>
<p>@pastedText</p>
@code {
private string? pastedText;
private void CopyToClipboard()
{
Clipboard.SetText("https://mythetech.com");
}
private void PasteFromClipboard()
{
pastedText = Clipboard.GetText();
}
}IClipboard Interface (Dependency Injection)
For Blazor applications, Hermes also provides an IClipboard interface that integrates with dependency injection. This is the preferred approach in Blazor components since it supports async operations and is easy to mock in tests.
public interface IClipboard
{
Task SetTextAsync(string text, CancellationToken ct = default);
Task<string?> GetTextAsync(CancellationToken ct = default);
}IClipboard is automatically registered in the DI container when using HermesBlazorAppBuilder, so no additional setup is needed.
Blazor Example (DI)
@inject IClipboard Clipboard
<button @onclick="CopyToClipboard">Copy Link</button>
<button @onclick="PasteFromClipboard">Paste</button>
<p>@pastedText</p>
@code {
private string? pastedText;
private async Task CopyToClipboard()
{
await Clipboard.SetTextAsync("https://mythetech.com");
}
private async Task PasteFromClipboard()
{
pastedText = await Clipboard.GetTextAsync();
}
}When to Use Which
| Approach | When to use |
|---|---|
IClipboard (DI) | Blazor components, anywhere with DI access, testable code |
Clipboard (static) | Non-DI contexts, console apps, quick scripts |
Next Steps
- WebView Interop — sending clipboard data between .NET and the frontend
- Tray Applications — clipboard operations in tray-based workflows
- Key-Value Store — persisting data across sessions
