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.
- 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.
- 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.
8 Answers
You can use System.Windows.Forms.Clipboard.SetText(..)
.
System.Windows.Forms.Clipboard.SetText
(Windows Forms) or System.Windows.Clipboard.SetText
(WPF)
Sharp Copier
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.
NoctisWPF: System.Windows.Clipboard
(PresentationCore.dll)
Sharp Copy Machines
Winforms: System.Windows.Forms.Clipboard
Windows Copy Clipboard
Both have a static SetText
method.
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.
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 MortensenIn Windows Forms, if your string is in a textbox, you can easily use this:
Peter MortensenUse 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