简体   繁体   中英

Programmatically add text to the clipboard in C#

Is there a way to programmatically add text to the clipboard in C# (3.5) or javascript ? Does the client machine type make a difference?

Edit: Sorry, forgot to mention am using asp.net.

You can use Clipboard.SetText(string) and Clipboard.GetText() to interact with the clipboard.

See the MSDN article

As long as the client supports .NET, it shouldn't make a difference.


Oh, for the web...

Then use something like this jQuery plugin:
http://plugins.jquery.com/project/clipboard

It will allow you to copy and paste from the clipboard across browsers.

C#: In System.Windows.Forms you have a Clipboard class and a SetText method which probably is what you're looking for. Example:

Clipboard.SetText("My text");

Edit: Since you're looking for a way to set clipboard text on a client computer, here's an updated answer:

There is no browser agnostic way of setting clipboard text. It's possible in Internet Explorer using the following snippet:

window.clipboardData.setData('Text', 'Your text');

In Firefox and Webkit browsers, for security reasons, you need to go via flash to set clipboard text. Zeroclipboard is a nice library for this.

In WinForms there is a static Clipboard class with a SetText method. So you can use

Clipboard.SetText("whatever");

There are some differences to the default text format based on which operating system you are running on: http://msdn.microsoft.com/en-us/library/ydby206k.aspx

As far as I'm aware in the web world it would be a security issue to access the clipboard without some sort of client side control, ie ActiveX control or Flash. So it would prompt for the user to allow the access. Here is something to get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM