Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy data to clipboard in C#

Tags:

c#

clipboard

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"?

like image 924
aharon Avatar asked Aug 23 '10 08:08

aharon


People also ask

How to copy data to clipboard in c#?

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.

How do I copy text to clipboard?

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.

How do I edit clipboard?

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.

How to use clipboard in asp net c#?

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."


2 Answers

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:

  1. In Solution Explorer, expand your console project.
  2. Right-click References, then click Add Reference...
  3. In the Assemblies group, under Framework, select System.Windows.Forms.
  4. Click OK.

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) {   // ... } 
like image 37
skia.heliou Avatar answered Sep 23 '22 02:09

skia.heliou


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?

like image 145
Kieren Johnstone Avatar answered Sep 25 '22 02:09

Kieren Johnstone