Clipboard
Read from and write to the operating-system clipboard - plain strings or typed data items such as HTML and images - through the asynchronous Clipboard API, from C#.
@inject Bit.Butil.Clipboard clipboardMDN reference
WriteText places a string on the system clipboard and completes once the copy has landed. Writing generally succeeds without a prompt when triggered from a user gesture such as a button click.
await clipboard.WriteText("Hello from C#!");ReadText resolves with the clipboard's current text content. Because reading exposes potentially sensitive data, the browser shows a permission prompt the first time and may require the page to be focused.
string text = await clipboard.ReadText();Write puts one or more ClipboardItem entries - a MIME type plus raw bytes - on the clipboard in a single operation. Pasting into a rich editor picks the HTML flavor, while plain-text targets receive the text/plain flavor. This demo writes both flavors of the same message.
var plain = new ClipboardItem
{
MimeType = "text/plain",
Data = Encoding.UTF8.GetBytes("Bit.Butil"),
};
var html = new ClipboardItem
{
MimeType = "text/html",
Data = Encoding.UTF8.GetBytes("<b>Bit.Butil</b>"),
};
await clipboard.Write([plain, html]);Read returns every representation currently on the clipboard as ClipboardItem entries. Pass a ClipboardFormats instance to request specific unsanitized formats (for example raw text/html) where the browser supports it. Copy something - text, rich text or an image - then read it back.
ClipboardItem[] items = await clipboard.Read();
foreach (var item in items)
{
// item.MimeType, item.Data (byte[])
}
// request unsanitized HTML where supported:
var unsanitized = await clipboard.Read(new ClipboardFormats
{
Unsanitized = ["text/html"],
});API reference
ValueTask WriteText(string text)ValueTask<string> ReadText()ValueTask Write(ClipboardItem[] items)ValueTask<ClipboardItem[]> Read(ClipboardFormats? formats = null)string MimeTypebyte[] Datastring[] Unsanitized