Paste Files from Clipboard with Cut or Copy

牧云@^-^@ 提交于 2019-12-10 15:48:13

问题


The .NET Clipboard class has methods to put files into the clipboard and also define if they should be moved or copied (cut/copy).

But if I want to paste files that were copied into the clipboard, I see no way to find out if the file was cut or copied with standard Clipboard methods.


回答1:


The information is stored in a Clipboard data object named "Preferred DropEffect". A memory stream containing a 4-byte-array contains the enum value for System.Windows.DragDropEffects in the first byte:

public static void PasteFilesFromClipboard(string aTargetFolder)
{
    var aFileDropList = Clipboard.GetFileDropList();
    if (aFileDropList == null || aFileDropList.Count == 0) return;

    bool aMove = false;

    var aDataDropEffect = Clipboard.GetData("Preferred DropEffect");
    if (aDataDropEffect != null)
    {
        MemoryStream dropEffect = (MemoryStream)aDataDropEffect;
        byte[] moveEffect = new byte[4];
        dropEffect.Read(moveEffect, 0, moveEffect.Length);
        aMove = moveEffect[0] == 2;
    }

    foreach (string aFileName in aFileDropList)
    {
        if (aMove) { } // Move File ...
        else { } // Copy File ...
    }
}

[Flags]
public enum DragDropEffects
{
    Scroll = int.MinValue,
    All = -2147483645,
    None = 0,
    Copy = 1,
    Move = 2,
    Link = 4
}


来源:https://stackoverflow.com/questions/57787840/paste-files-from-clipboard-with-cut-or-copy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!