简体   繁体   中英

Paste Files from Clipboard with Cut or Copy

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.

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 :

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 aDropEffect = (MemoryStream)aDataDropEffect;
            byte[] aMoveEffect = new byte[4];
            aDropEffect.Read(aMoveEffect, 0, aMoveEffect.Length);
            var aDragDropEffects = (DragDropEffects)BitConverter.ToInt32(aMoveEffect, 0);
            aMove = aDragDropEffects.HasFlag(DragDropEffects.Move);
    }

    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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM