How to copy data to clipboard in C#

我如何将字符串(例如“hello”)复制到C#中的系统剪贴板中,所以下次按CTRL + V时,我会得到“hello”?


You'll need a namespace declaration:

using System.Windows.Forms;

OR for WPF:

using System.Windows;

To copy an exact string (literal in this case):

Clipboard.SetText("Hello, clipboard");

To copy the contents of a textbox:

Clipboard.SetText(txtClipboard.Text);

See here for an example. Or... Official MSDN documentation or Here for WPF.


Clipboard.SetText("hello");

你需要使用System.Windows.FormsSystem.Windows命名空间。


My Experience with this issue using WPF C# coping to clipboard and System.Threading.ThreadStateException is here with my code that worked correctly with all browsers:

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

credits to this post here

But this works only on localhost, so don't try this on a server, as it's not going to work.

On server-side, I did it by using zeroclipboard . The only way, after a lot of research.

链接地址: http://www.djcxy.com/p/19704.html

上一篇: 如何使用Firefox或Chrome手动触发HTTP POST请求?

下一篇: 如何在C#中将数据复制到剪贴板