C# – Copy files to clipboard in C#

cclipboardwinforms

I have a Windows Forms TreeView (node, subnodes). Each node contains some additional information in its Tag. Also, each nodes maps a file on the disk. What's the easiest way copy/cut/paste nodes/files in C#?

It would be nice to have some sample code.

Best Answer

Consider using the Clipboard class. It features all the methods necessary for putting data on the Windows clipboard and to retrieve data from the Windows clipboard.

StringCollection paths = new StringCollection();
paths.Add("f:\\temp\\test.txt");
paths.Add("f:\\temp\\test2.txt");
Clipboard.SetFileDropList(paths);

The code above will put the files test.txt and test2.txt for copy on the Windows Clipboard. After executing the code you can navigate to any folder and Paste (Ctrl+V) the files. This is equivalent to selecting both files in Windows Explorer and selecting copy (Ctrl+C).