如何以符合系统标准的形式以编程方式从Windows剪贴板剪切/复制文件/从Windows剪贴板获取文件? [英] How to programmatically cut/copy/get files to/from the Windows clipboard in a system standard compliant form?

查看:123
本文介绍了如何以符合系统标准的形式以编程方式从Windows剪贴板剪切/复制文件/从Windows剪贴板获取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 如何将对特定文件和/或文件夹的剪切/复制引用放入Windows剪贴板,以便在打开标准Windows资源管理器窗口时,转到某个位置并按 Ctrl + V -是否粘贴了文件?

  1. How do I put a cut/copy reference to specific files and/or folders into the Windows clipboard so that when I open standard Windows Explorer window, go to somewhere and press Ctrl + V - the files are pasted?

如果我在Windows中复制或剪切了一些文件/文件夹资源管理器,如何在程序中获取此信息(全名以及是否被剪切或复制)?

If I copy or cut some files/folders in Windows Explorer, how do I get this information (full names and whether they were cut or copied) in my program?

我使用 C#4.0 进行编程,但是其他语言的方式也很有趣。 / p>

I program in C# 4.0, but other languages' ways are also interesting to know.

推荐答案

我有90%的解决方案,它是从剪贴板格式反向工程的,而我的回答则在此线程。您需要设置两个剪贴板数据。文件列表,这很容易做到。还有另一种剪贴板格式,名为 Preferred Dropeffect,指示是否请求复制或移动文件。导致该代码:

I've got the 90% solution, reverse-engineered from the clipboard formats and my answer in this thread. You'll need to set two pieces of clipboard data. The list of files, that's easy to do. And another clipboard format named "Preferred Dropeffect" that indicates whether a copy or a move of the files is requested. Leading to this code:

    public static void StartCopyFiles(IList<string> files, bool copy) {
        var obj = new DataObject();
        // File list first
        var coll = new System.Collections.Specialized.StringCollection();
        coll.AddRange(files.ToArray());
        obj.SetFileDropList(coll);
        // Then the operation
        var strm = new System.IO.MemoryStream();
        strm.WriteByte(copy ? (byte)DragDropEffects.Copy : (byte)DragDropEffects.Move);
        obj.SetData("Preferred Dropeffect", strm);
        Clipboard.SetDataObject(obj);
    }

示例用法:

        var files = new List<string>() { @"c:\temp\test1.txt", @"c:\temp\test2.txt" };
        StartCopyFiles(files, true);

在Windows资源管理器中按Ctrl + V可以从我的c:\temp目录复制文件。

Pressing Ctrl+V in Windows Explorer copied the files from my c:\temp directory.

我无法进行的是剪切操作,将false传递给StartCopyFiles()产生了复制操作,原始文件未从源目录中删除。不知道为什么,应该工作。我认为首选DropEffects的实际流格式更奇特,可能涉及臭名昭著的PIDL。

What I could not get going is the "cut" operation, passing false to StartCopyFiles() produced a copy operation, the original files where not removed from the source directory. No idea why, should have worked. I reckon that the actual stream format of "Preferred DropEffects" is fancier, probably involving the infamous PIDLs.

这篇关于如何以符合系统标准的形式以编程方式从Windows剪贴板剪切/复制文件/从Windows剪贴板获取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆