How can I copy a string (e.g "hello") to the System Clipboard in C#, so next time I press CTRL+V I'll get "hello"?
If you use CTRL+C, some data or files are copied to the system clipboard memory and when you use CTRL+V, the data is copied back to wherever you paste it. The Clipboard class provides functionality to place data to and retrieve data from the clipboard. Clipboard has static methods to copy and paste data.
Select the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want. The Office Clipboard can hold up to 24 items.
When you long-press and copy text to save to the clipboard, it appears in the Clipboard log inside the app. Press the three dots to the right of the clipboard snippet to open a menu with more options. In this menu, you can View, Edit, Share, or Select that entry so that you can paste it anywhere you like.
NET-C# Copying the label text using LinkButton to clipboard says "Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it."
For console projects in a step-by-step fashion, you'll have to first add the System.Windows.Forms
reference. The following steps work in Visual Studio Community 2013 with .NET 4.5:
System.Windows.Forms
.Then, add the following using
statement in with the others at the top of your code:
using System.Windows.Forms;
Then, add either of the following Clipboard
.SetText
statements to your code:
Clipboard.SetText("hello"); // OR Clipboard.SetText(helloString);
And lastly, add STAThreadAttribute
to your Main
method as follows, to avoid a System.Threading.ThreadStateException
:
[STAThreadAttribute] static void Main(string[] args) { // ... }
There are two classes that lives in different assemblies and different namespaces.
WinForms: use following namespace declaration, make sure Main
is marked with [STAThread]
attribute:
using System.Windows.Forms;
WPF: use following namespace declaration
using System.Windows;
console: add reference to System.Windows.Forms
, use following namespace declaration, make sure Main
is marked with [STAThread]
attribute. Step-by-step guide in another answer
using System.Windows.Forms;
To copy an exact string (literal in this case):
Clipboard.SetText("Hello, clipboard");
To copy the contents of a textbox either use TextBox.Copy() or get text first and then set clipboard value:
Clipboard.SetText(txtClipboard.Text);
See here for an example. Or... Official MSDN documentation or Here for WPF.
Remarks:
Clipboard is desktop UI concept, trying to set it in server side code like ASP.Net will only set value on the server and has no impact on what user can see in they browser. While linked answer lets one to run Clipboard access code server side with SetApartmentState
it is unlikely what you want to achieve.
If after following information in this question code still gets an exception see "Current thread must be set to single thread apartment (STA)" error in copy string to clipboard
This question/answer covers regular .NET, for .NET Core see - .Net Core - copy to clipboard?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With