C#
clipboard
string manipulation
code example
duplicate question

How do I copy the contents of a String to the clipboard in C?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In .NET, copying text to the clipboard is simple once you are using the correct Windows clipboard API and the calling thread is in STA mode. Most confusion comes from mixing plain C with C#, or from trying to call clipboard APIs from a console app without the required desktop thread model. The reliable answer for a Windows C# application is usually Clipboard.SetText.

The Usual C# Answer

For a Windows Forms application, use System.Windows.Forms.Clipboard.SetText.

csharp
1using System;
2using System.Windows.Forms;
3
4public static class Program
5{
6    [STAThread]
7    public static void Main()
8    {
9        string text = "Copied from C#";
10        Clipboard.SetText(text);
11        Console.WriteLine("Clipboard updated.");
12    }
13}

Two pieces matter here:

  • 'Clipboard.SetText writes the string to the Windows clipboard.'
  • '[STAThread] makes the main thread compatible with COM-based clipboard operations.'

Without STA, the clipboard call may throw a threading-related exception.

Why STA Matters

The Windows clipboard is tied to desktop APIs that expect a single-threaded apartment. Graphical application templates usually give you that automatically, but console utilities often do not.

That is why code that works in a WinForms app can fail in a console project until you add [STAThread] and reference the right desktop assembly.

If the project targets modern .NET, you may also need Windows desktop support enabled in the project file.

WPF Uses a Similar API

In WPF, the Clipboard class lives under System.Windows, but the idea is the same.

csharp
1using System;
2using System.Windows;
3
4public class Program
5{
6    [STAThread]
7    public static void Main()
8    {
9        Clipboard.SetText("Copied from WPF");
10        Console.WriteLine("Done.");
11    }
12}

If your application is already WPF-based, use the WPF clipboard API rather than adding a Windows Forms dependency only for clipboard access.

Console Applications Need Extra Care

A console tool can still copy text to the clipboard, but it needs to be a Windows-only utility because the built-in clipboard APIs are desktop APIs.

csharp
1using System;
2using System.Windows.Forms;
3
4public static class Program
5{
6    [STAThread]
7    public static void Main(string[] args)
8    {
9        string value = args.Length > 0 ? args[0] : "default text";
10        Clipboard.SetText(value);
11        Console.WriteLine("Copied to clipboard.");
12    }
13}

This is a good fit for internal developer tools, but it is not a cross-platform clipboard solution.

Handle Empty Input and Busy Clipboard States

Clipboard access is not guaranteed to succeed on the first try. Another process may temporarily hold the clipboard. Validate the input and retry if the operation is transiently blocked.

csharp
1using System;
2using System.Threading;
3using System.Windows.Forms;
4
5public static class Program
6{
7    [STAThread]
8    public static void Main()
9    {
10        string text = "Retry example";
11
12        if (string.IsNullOrWhiteSpace(text))
13        {
14            Console.WriteLine("Nothing to copy.");
15            return;
16        }
17
18        for (int attempt = 0; attempt < 5; attempt++)
19        {
20            try
21            {
22                Clipboard.SetText(text);
23                Console.WriteLine("Copied.");
24                return;
25            }
26            catch
27            {
28                Thread.Sleep(100);
29            }
30        }
31
32        Console.WriteLine("Clipboard unavailable.");
33    }
34}

That is usually enough for desktop utilities. If repeated failures matter, log the exception instead of swallowing it completely.

If You Truly Mean the C Language

The title says "in C," but the tags point to C#. That distinction matters. There is no standard C clipboard library in the language itself. In plain C, clipboard access is platform-specific and usually means calling native APIs such as Win32 on Windows.

So the short version is:

  • in C#, use Clipboard.SetText
  • in plain C, use the operating system's native clipboard APIs

Security and UX Considerations

Copying to the clipboard can overwrite data the user was intentionally keeping there. If the text may be sensitive, do not silently copy it during background work. Trigger the action explicitly from a button or command so the user knows what happened.

Common Pitfalls

  • Forgetting [STAThread], which causes clipboard calls to fail in console and desktop entry points.
  • Mixing up C# and plain C, even though the solution is platform-specific in C and framework-specific in C#.
  • Referencing the wrong clipboard namespace for the project type.
  • Assuming clipboard access is always immediate even though another process may temporarily lock it.
  • Using Windows-only clipboard APIs in code that is supposed to run cross-platform.

Summary

  • In Windows C#, the normal solution is Clipboard.SetText.
  • The calling thread should be STA, which is why [STAThread] is important.
  • WinForms and WPF expose similar clipboard APIs from different namespaces.
  • Console apps can use the clipboard too, but only in a Windows desktop context.
  • If you truly mean plain C rather than C#, you need operating-system-specific clipboard APIs instead.

Course illustration
Course illustration

All Rights Reserved.