Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set clipboard to copy files?

Tags:

c#

.net

In my application, I allow the user to select items that correspond to files on a disk. When the user presses Ctrl+C, I want the file to be sent to the clipboard, where the user can then paste the file somewhere else.

I want to implement it in a way so that the user can copy, but not paste inside my application. The user is then free to paste the file into instances of Explorer or other applications that will accept the file from the clipboard.

I know how to set information in the clipboard, just not how to set it so that Windows recognizes it as a copy operation for certain files.

How can I accomplish this?

like image 803
Joan Venge Avatar asked Dec 02 '22 07:12

Joan Venge


2 Answers

To catch the CTRL + C you can check the Keys Pressed on the KeyPress event. And to copy the file(s) use something similar to below:

private void CopyFile(string[] ListFilePaths)
{
  System.Collections.Specialized.StringCollection FileCollection = new System.Collections.Specialized.StringCollection();

  foreach(string FileToCopy in ListFilePaths)
  {
    FileCollection.Add(FileToCopy);
  }

  Clipboard.SetFileDropList(FileCollection);
}
like image 160
Lloyd Powell Avatar answered Dec 04 '22 22:12

Lloyd Powell


Simply use the Clipboard class:
http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx

Use the SetFileDropList method to make Windows recognize it as a copy operation:
http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.setfiledroplist.aspx

If the data in your application isn't based on actual files, I suggest you first generate them as temporary files in the user's temp folder, and add those to the filedroplist.

like image 26
Zyphrax Avatar answered Dec 04 '22 21:12

Zyphrax