Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
1.42/5 (4 votes)
See more:
I can get list of files from clipboard but i cant detect whether this data is for moving or copy?

when user click on both "cut" and "paste" option data is set into clipboard and we can retrieve it using some code but how we can simultaneously get whether this clipboard data is for copy or cut?

What I have tried:

I can get list of files from clipboard using this code:
IDataObject data = Clipboard.GetDataObject();
if (!data.GetDataPresent(DataFormats.FileDrop))
return;
string[] clipboardData = (string[])data.GetData(DataFormats.FileDrop);
Posted
Updated 6-Sep-22 2:28am
Comments
RickZeeland 8-Apr-16 16:34pm    
Did you downvote solution 1 and 2 ?

When copying a file it sets the Copy and Link flags, while for a cut it sets the Move bit flag.
See this for more details about "Preferred DropEffect"
Shell Clipboard Formats - Win32 apps | Microsoft Learn[^]
C#
[Flags]
public enum NativeDropEffect : uint
{
    None = 0x00000000,
    Copy = 0x00000001,
    Move = 0x00000002,
    Link = 0x00000004,
    Scroll = 0x80000000
}

IDataObject iData = System.Windows.Forms.Clipboard.GetDataObject();
var fileDrop = (string[])iData.GetData(DataFormats.FileDrop);
NativeDropEffect dropEffect;
using (var reader = new BinaryReader((MemoryStream)iData.GetData("Preferred DropEffect")))
{
    dropEffect = (NativeDropEffect)reader.ReadUInt32();
}

if(dropEffect.HasFlag(NativeDropEffect.Copy))
{
//copy
}
else if(dropEffect.HasFlag(NativeDropEffect.Move))
{
//cut
}

Besides explorer setting this flag you can also set it yourself when you copy a FileDrop to the clipboard programatically and explorer will honour it and move or copy the files depending on the flags.
 
Share this answer
 
v2
No way. And you never need it. First of all, there is no such distinction. The question simply makes no sense.

Let me also explain that: there is no such thing as "clipboard move". This is a pure application-side part. You just missed the idea. Yes, some applications offer "cut file — past file" operations which is functionally equivalent to "move", but this is nothing but a metaphor. In the OS, there is no such thing.

One example is "moving files". This is how it works: first, application handles "cut files" command and simply mark some files scheduled for the move. It's important to understand that nothing is cut. Next command is supposed to be "paste files". If it is issued before any other clipboard operation, the files are simply moved. If the user does something else, the "cut" command is simply ignored; and the files stay where they were. If the user issues that command, the files are moved. Same things happen to the cells of a spreadsheet, including Excel.

You can develop the same technique in your application. The question is: is the system clipboard even involved? The answer is: yes and no. As in both examples explained in the previous paragraph, your application can be executed in one instance, of several instances of the application process. If it was always the same instance, using the clipboard would be pointless. But the same application can be executed in several different processes; and the processes are isolated. They need to communicate on this file copy/paste/move activity; and this is conveniently done via the available system clipboard. All you need to implement similar thing is little logical reasoning.

Moreover, as to the file manipulation: you can have multiple unrelated application using Shell API. Not only they are different processes, but even the processes of different applications. And they can communicate through the system clipboard, in this aspect.

That's all.

—SA
 
Share this answer
 
This is not possible with Clipboard, it's determined by the application that uses Clipboard. Sorry !
 
Share this answer
 
When you use file drop, you just check the shift and control keys:
C#
if ((ModifierKeys & Keys.Shift) != 0)
    {
    // It's a move operation.
    ...
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900