简体   繁体   中英

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?

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

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.

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