Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy byte array into clipboard

I'm trying to do a client-server program in which it is possible to share the content of the clipboard.

Right now I am able to share it if the content type is audio, image or text. The idea is that I convert the content in a byte array, send it, convert it back in its original type (Stream, BitmapSource or string) and inject it in the client clipboard by using the methods Clipboard.SetAudio, Clipboard.SetImage or Clipboard.SetText.

My problem is when there are some files in the clipboard. I use the method Clipboard.GetFileDropList to get a list of the files, and for each file in the list I convert it in a byte array and send it to the client. How can I inject this byte array in the client clipboard?

I know there is the method Clipboard.SetFileDropList, but it requires me to provide a file list and since the file does not exist on the client I cannot use it.

How can I solve this problem?

like image 373
Nicola Gallo Avatar asked Oct 20 '22 14:10

Nicola Gallo


1 Answers

In order to make the client treat the files as pastable, they'll need to exist on the client filesystem in some way, since the clipboard expects a list of filenames when setting clipboard content.

This can be done by transferring the data as a stream to your client, and then making the client immediately unpack that stream to a temp folder, the path to which is obtainable via:

var temp = Environment.ExpandEnvironmentVariables("%TEMP%");

Once done and the files are i place, you can position those files on the clipboard as if they were the ones copied.

Be warned that supporting file copy/paste instead of having an option to "transfer" files could run much slower than other operations, due to how big files can get.

like image 94
David Avatar answered Oct 23 '22 06:10

David