C# 将文件剪切到剪贴板
我正在寻找一种以编程方式将文件剪切到剪贴板的方法,例如,对 C# 中的函数进行一些调用,其作用与在 Windows 资源管理器 并按 Ctrl + X。
运行程序并在硬盘上的其他文件夹中按 Ctrl + V 后,原始文件将移动在 C# 中将文件复制到剪贴板,我知道很容易做到复制工作,但切割似乎工作不同。我该怎么做?
I'm looking for a way to programmatically cut a file to the clipboard, for example, some call to a function in C# that does the same as selecting a file in the Windows Explorer and pressing Ctrl + X.
After running the program and pressing Ctrl + V in some other folder on the hard drive, the original file would be moved to the new folder. By looking at Stack Overflow question Copy files to clipboard in C#, I know that it's easy to do the copy job, but cutting seems to work different. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请尝试以下内容,翻译自代码项目文章 在VB.NET中使用DropEffect设置剪贴板文件DropList:
Please try the following, translated from The Code Project article Setting the Clipboard File DropList with DropEffect in VB.NET:
只是为了看看会发生什么,我替换了 MemoryStream 与 DragDropEffects 像这样:
显然,它的工作原理是真正的剪切而不是副本! (这是在 Windows 7 上 - 我没有尝试过其他操作系统)。不幸的是,它的作用只是巧合。例如,
不会产生副本(仍然是剪切)。看来非空会导致剪切,空会导致复制。
Just to see what happens, I replaced the MemoryStream with a DragDropEffects like this:
Apparently, it works as a genuine cut rather than a copy! (This was on Windows 7 - I have not tried other operating systems). Unfortunately, it works only coincidentally. For example,
does not yield a copy (still a cut). It seems that a non-null causes a cut, a null a copy.
我喜欢将这样的代码包装在有意义的 API 中。我也喜欢尽可能避免魔法字节字符串。
我想出了这个扩展方法,有效地使用了
DragDropEffects
枚举,解决了 @Keith 在他的答案中面临的谜团。I like to wrap code like this in an API that makes sense. I also like to avoid magic strings of bytes where I can.
I came up with this extension method that solves the mystery that @Keith was facing in his answer, effectively using the
DragDropEffects
enum.