Joan Venge
Joan Venge

Reputation: 330872

How to set clipboard to copy files?

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?

Upvotes: 6

Views: 7593

Answers (2)

Lloyd Powell
Lloyd Powell

Reputation: 18770

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);
}

Upvotes: 12

Yvo
Yvo

Reputation: 19263

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.

Upvotes: 7

Related Questions