C# 将文件剪切到剪贴板

发布于 2024-08-18 06:16:11 字数 423 浏览 5 评论 0原文

我正在寻找一种以编程方式将文件剪切到剪贴板的方法,例如,对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

有深☉意 2024-08-25 06:16:11

请尝试以下内容,翻译自代码项目文章 在VB.NET中使用DropEffect设置剪贴板文件DropList

byte[] moveEffect = new byte[] {2, 0, 0, 0};
MemoryStream dropEffect = new MemoryStream();
dropEffect.Write(moveEffect, 0, moveEffect.Length);

DataObject data = new DataObject();
data.SetFileDropList(files);
data.SetData("Preferred DropEffect", dropEffect);

Clipboard.Clear();
Clipboard.SetDataObject(data, true);

Please try the following, translated from The Code Project article Setting the Clipboard File DropList with DropEffect in VB.NET:

byte[] moveEffect = new byte[] {2, 0, 0, 0};
MemoryStream dropEffect = new MemoryStream();
dropEffect.Write(moveEffect, 0, moveEffect.Length);

DataObject data = new DataObject();
data.SetFileDropList(files);
data.SetData("Preferred DropEffect", dropEffect);

Clipboard.Clear();
Clipboard.SetDataObject(data, true);
狠疯拽 2024-08-25 06:16:11

只是为了看看会发生什么,我替换了 MemoryStreamDragDropEffects 像这样:

data.SetData("FileDrop", files);
data.SetData("Preferred DropEffect", DragDropEffects.Move);

显然,它的工作原理是真正的剪切而不是副本! (这是在 Windows 7 上 - 我没有尝试过其他操作系统)。不幸的是,它的作用只是巧合。例如,

data.SetData("Preferred DropEffect", DragDropEffects.Copy);

不会产生副本(仍然是剪切)。看来非空会导致剪切,空会导致复制。

Just to see what happens, I replaced the MemoryStream with a DragDropEffects like this:

data.SetData("FileDrop", files);
data.SetData("Preferred DropEffect", DragDropEffects.Move);

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,

data.SetData("Preferred DropEffect", DragDropEffects.Copy);

does not yield a copy (still a cut). It seems that a non-null causes a cut, a null a copy.

跨年 2024-08-25 06:16:11

我喜欢将这样的代码包装在有意义的 API 中。我也喜欢尽可能避免魔法字节字符串。

我想出了这个扩展方法,有效地使用了 DragDropEffects 枚举,解决了 @Keith 在他的答案中面临的谜团。

public static class Extensions
{
    public static void PutFilesOnClipboard(this IEnumerable<FileSystemInfo> filesAndFolders, bool moveFilesOnPaste = false)
    {
        var dropEffect = moveFilesOnPaste ? DragDropEffects.Move : DragDropEffects.Copy;

        var droplist = new StringCollection();
        droplist.AddRange(filesAndFolders.Select(x=>x.FullName).ToArray());     

        var data = new DataObject();
        data.SetFileDropList(droplist);
        data.SetData("Preferred Dropeffect", new MemoryStream(BitConverter.GetBytes((int)dropEffect)));
        Clipboard.SetDataObject(data);
    }
}

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.

public static class Extensions
{
    public static void PutFilesOnClipboard(this IEnumerable<FileSystemInfo> filesAndFolders, bool moveFilesOnPaste = false)
    {
        var dropEffect = moveFilesOnPaste ? DragDropEffects.Move : DragDropEffects.Copy;

        var droplist = new StringCollection();
        droplist.AddRange(filesAndFolders.Select(x=>x.FullName).ToArray());     

        var data = new DataObject();
        data.SetFileDropList(droplist);
        data.SetData("Preferred Dropeffect", new MemoryStream(BitConverter.GetBytes((int)dropEffect)));
        Clipboard.SetDataObject(data);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文