将文件复制到C#中剪贴板

问题描述:

我有一个 Windows窗体 TreeView控件(节点,子节点)。每个节点包含在其标签一些额外的信息。此外,每个节点映射磁盘上的文件。什么是C#中的最简单的方法复制/剪切/粘贴节点/文件?

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#?

这将是不错的一些示例code。

It would be nice to have some sample code.

考虑使用的 Clipboard类。它具有所有必要从Windows剪贴板穿着Windows剪贴板数据和检索数据的方法。

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

在code以上将投入的test.txt和的test2.txt对Windows剪贴板复制的文件。执行code后,您可以导航到任何文件夹和粘贴(Ctrl + V键)的文件。这相当于在Windows资源管理器中选择这两个文件,​​并选择复制(Ctrl + C)。

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