Active5 months ago

If I have some text in a String, how can I copy that to the clipboard so that the user can paste it into another window (for example, from my application to Notepad)?

Also, each nodes maps a file on the disk. What's the easiest way copy/cut/paste nodes/files in C#? Cut files to clipboard in C#. Paste files from the clipboard. The example Paste files from the clipboard in C# shows how a program can paste files from the clipboard. You can copy the files to the clipboard by selecting them in Windows Explorer and pressing Ctrl+C. This example shows how a C# program can use code to copy files to the clipboard. As you saw in the previous example, a list of files copied to the clipboard is actually an array of file names.

Copy
  1. The Clipboard class provides functionality to place and retrieve data from the system clipboard. Usually Usually If you use CTRL+C, some data or files are copied to a system clipboard and stored in system memory and when you use CTRL+V, the data is copied back to wherever you paste it. The Clipboard class provides functionality to.
  2. I'm looking for a way to programmatically cut a file to the clipboard. Cut files to clipboard in C#. Programatically pasting files from clipboard: Copy.
Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
ElieElie
9,63321 gold badges65 silver badges122 bronze badges

8 Answers

You can use System.Windows.Forms.Clipboard.SetText(..).

bluish
15.1k19 gold badges95 silver badges154 bronze badges
ravuyaravuya
7,2883 gold badges25 silver badges31 bronze badges

System.Windows.Forms.Clipboard.SetText (Windows Forms) or System.Windows.Clipboard.SetText (WPF)

Peter Mortensen

Sharp Copier

14.4k19 gold badges88 silver badges117 bronze badges
Jeff MoserJeff MoserClipboard
16.9k5 gold badges56 silver badges82 bronze badges

I wish calling SetText were that easy but there are quite a few gotchas that you have to deal with. You have to make sure that the thread you are calling it on is running in the STA. It can sometimes fail with an access denied error then work seconds later without problem - something to do with the COM timing issues in the clipboard. And if your application is accessed over Remote Desktop access to the clipboard is sketchy. We use a centralized method to handle all theses scenarios instead of calling SetText directly.

@Stecy: Here's our centralized code:

The StaHelper class simply executes some arbitrary code on a thread in the Single Thread Apartment (STA) - required by the clipboard.

How To Open Copy Clipboard

Then we have a specific class for setting text on the clipboard. Creating a DataObject manually is required in some edge cases on some versions of Windows/.NET. I don't recall the exact scenarios now and it may not be required in .NET 3.5.

Usage looks like this: Paragon hfs for windows cracker.

Noctis
9,9503 gold badges32 silver badges70 bronze badges
Paul AlexanderPaul Alexander
25.6k11 gold badges87 silver badges139 bronze badges

WPF: System.Windows.Clipboard (PresentationCore.dll)

Sharp Copy Machines

Winforms: System.Windows.Forms.Clipboard

Windows Copy Clipboard

Both have a static SetText method.

bluish
15.1k19 gold badges95 silver badges154 bronze badges
bsneezebsneeze

This works for me:

You want to do:

But it causes an error saying it must be in a single thread of ApartmentState.STA.

So let's make it run in such a thread. The code for it:

After calling copy_to_clipboard(), the string is copied into the clipboard, so you can Paste or Ctrl + V and get back the string as String to be copied to the clipboard.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
benli1212benli1212

Using the solution showed in this question, System.Windows.Forms.Clipboard.SetText(..), results in the exception:

Current thread must be set to single thread apartment (STA) mode before OLE calls can be made

To prevent this, you can add the attribute:

to

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
LeroyLeroy

In Windows Forms, if your string is in a textbox, you can easily use this:

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Magnetic_dudMagnetic_dud
1,0782 gold badges19 silver badges34 bronze badges

Use try-catch, even if it has an error it will still copy.

If you use a message box to capture the exception, it will show you error, but the value is still copied to clipboard.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Abu FaisalAbu Faisal

Not the answer you're looking for? Browse other questions tagged c#.netclipboard or ask your own question.

p2umi.netlify.com – 2018