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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/211611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:17:33  来源:igfitidea点击:

Copy files to clipboard in C#

c#winformsclipboard

提问by

I have a Windows FormsTreeView(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#?

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

It would be nice to have some sample code.

有一些示例代码会很好。

回答by Jorge Ferreira

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.

考虑使用Clipboard 类。它具有将数据放入 Windows 剪贴板和从 Windows 剪贴板检索数据所需的所有方法。

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

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

回答by AR.

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:

如果您只是在应用程序中复制和粘贴,则可以将树视图的剪切/复制操作映射到仅克隆所选节点的方法。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 IDataObjectinterface. I can't remember the source but here's something I had in my own notes:

如果您希望能够粘贴到其他应用程序,则必须使用剪贴板。通过了解有关IDataObject接口的更多信息,您可以获得比纯文本更有趣的东西。我不记得出处了,但这是我自己笔记中的一些内容:

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.

在类中实现时,IDataObject 方法允许用户在类的实例中以多种格式存储数据。以多种格式存储数据会增加目标应用程序(您可能不知道其格式要求)检索存储数据的机会。要将数据存储在 IDataObject 的实例中,请调用 SetData 方法并在 format 参数中指定数据格式。如果您不希望在检索时将存储的数据转换为另一种格式,请将 autoConvert 参数设置为 false。在 IDataObject 的一个实例上多次调用 SetData 以以一种以上的格式存储数据。

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

一旦您填充了一个实现IDataObject的对象 (例如称为yourTreeNodeDataObject的对象),您就可以调用:

Clipboard.SetDataObjecT(yourTreeNodeDataObject);