Saturday, February 12, 2011

Copy files to clipboard in C#

I have a WinForms TreeView (node, subnodes). Each node contains in it's Tag some additional info. Also, each nodes maps a file on the disk. What's the easiest way copy/cut/paste nodes/files in C#? Would be nice to have some sample code. Thanks

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

    smink : 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.
    smink : 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.
    From smink
  • 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);
    
    From AR
  • this is sweet! thanks so much!

    um...how would you cut a file onto the clipboard?

    Cheers, Mark

    From Mark

0 comments:

Post a Comment