Copy files to clipboard in C#
Asked Answered
I

2

31

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.

Ischia answered 17/10, 2008 at 10:3 Comment(0)
S
58

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).

Schizothymia answered 17/10, 2008 at 10:7 Comment(2)
Clipboard is only a placeholder. When data is transferred to the clipboard there is no information about whether this is a copy or a cut operation. It is the responsibility of the calling operation to distinguish one from the other based on saved state.Schizothymia
For example when you cut on Word the text cutted is immediately removed from the document. The cutted text is placed on the Windows clipboard and can then be pasted on demand.Schizothymia
P
5

If you are only copying and pasting within your application, you can map the cut/copy operation of your treeview to a method that just clones your selected node. Ie:

TreeNode selectedNode;
TreeNode copiedNode;

selectedNode = yourTreeview.SelectedNode;

if (selectedNode != null)
{
    copiedNode = selectedNode.Clone;
}

// Then you can do whatever you like with copiedNode elsewhere in your app.

If you are wanting to be able to paste to other applications, then you'll have to use the clipboard. You can get a bit fancier than just plain text by learning more about the IDataObject interface. I can't remember the source but here's something I had in my own notes:

When implemented in a class, the IDataObject methods allow the user to store data in multiple formats in an instance of the class. Storing data in more than one format increases the chance that a target application, whose format requirements you might not know, can retrieve the stored data. To store data in an instance of IDataObject, call the SetData method and specify the data format in the format parameter. Set the autoConvert parameter to false if you do not want stored data to be converted to another format when it is retrieved. Invoke SetData multiple times on one instance of IDataObject to store data in more than one format.

Once you've populated an object that implements IDataObject (e.g. something called yourTreeNodeDataObject), then you can call:

Clipboard.SetDataObjecT(yourTreeNodeDataObject);
Prosthodontics answered 17/10, 2008 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.