Coder Perfect

In C#, how can I copy info to the clipboard?


In C#, how can I transfer a string (for example, “hello”) to the System Clipboard such that when I click CTRL+V, I get “hello”?

Asked by aharon

Solution #1

There are two classes that are located in separate assemblies and namespaces.

To copy an exact string (literal in this example), use the following syntax:

Clipboard.SetText("Hello, clipboard");

Use TextBox to copy the contents of a textbox. Obtain text first, then set clipboard value: Copy() or get text first, then set clipboard value:


Take a look at this example. Official MSDN documentation or WPF documentation can be found here.


Answered by Kieren Johnstone

Solution #2

To do a step-by-step console project, you’ll need to first add the System. Windows. References to forms. The steps below work with.NET 4.5 and Visual Studio Community 2013:

Then, at the beginning of your code, combine the following using statement with the others:

using System.Windows.Forms;

Then, add one of the Clipboards below. Add the following SetText statements to your code:

// OR

Finally, to avoid a System, add STAThreadAttribute to your Main function as follows. Threading. ThreadStateException:

static void Main(string[] args)
  // ...

Answered by skia.heliou

Solution #3

My experience with this problem utilizing WPF C# clipboard and system coping. Threading. ThreadStateException is here, along with my code, which worked in all browsers:

Thread thread = new Thread(() => Clipboard.SetText("String to be copied to clipboard"));
thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA

Here are the sources for this article.

However, this only works on localhost; attempting to do it on a server will result in failure.

I used zeroclipboard on the server side to accomplish this. After a lot of res, the only way out was to

Answered by BMaximus

Solution #4


You’ll have to make use of the System. System or Windows.Forms For that, you’ll need to use Windows namespaces.

Answered by Bradley Smith

Solution #5

Clip.exe is a Windows program that allows you to configure the clipboard. Note that this does not apply to other operating systems outside Windows, which is still a pain.

        /// <summary>
        /// Sets clipboard to value.
        /// </summary>
        /// <param name="value">String to set the clipboard to.</param>
        public static void SetClipboard(string value)
            if (value == null)
                throw new ArgumentNullException("Attempt to set clipboard with null");

            Process clipboardExecutable = new Process(); 
            clipboardExecutable.StartInfo = new ProcessStartInfo // Creates the process
                RedirectStandardInput = true,
                FileName = @"clip", 

            clipboardExecutable.StandardInput.Write(value); // CLIP uses STDIN as input.
            // When we are done writing all the string, close it so clip doesn't wait and get stuck


Answered by QB kyu

Post is based on