C# Files - in clipboard How to set/read which operation is called - Cut or Copy

1.3k views Asked by At

I have read many topics about cut/copy to clipboard, but haven't found the answer for my problem

I'm working on a "File Manager" application like Windows Explorer. Files are listed in a listview, in details view.

I have CUT/COPY/PASTE operations, and I know how to use File.Move, .Copy, Clipboard.GetFileDropList(), .SetFileDropList()........... and it works great.

What I dont know is how and where can I write in memory (which method) and how can I read from memory (Clipboard) which operation is last used, Cut or Copy? Is there any string in memory windows explorer writes, that I can read and then know if it is CUT or COPY?

I want to let the user be able to cut/copy from my app to win Explorer and vice versa.

3

There are 3 answers

1
bitxwise On BEST ANSWER

If you want to determine which Clipboard operation was last called, I think you have to listen to Windows messages, particularly WM_CUT (0x0300) versus WM_COPY (0x0301), and track which was last sent/received. You can override the Control.WndProc method as discussed on MSDN.

Here are a few more reference links for Windows messages:

Clipboard messages

WM_CUT

WM_COPY

2
Øyvind Bråthen On

You use Clipboard.SetText to add text to the clipboard (Copy), and Clipboard.GetText to retrieve text from the clipboard (Paste).

There is an article here that should be able to help you out.

Also, regarding copy/paste of files, see this SO question:

0
NeoSvet On

Using this topic I wrote this code:

private const string DROP_EFFECT = "Preferred DropEffect";

private void CmdPaste()
{
  DataObject data = Clipboard.GetDataObject() as DataObject;
  var obj = data.GetData(DROP_EFFECT);
  bool isCut = false;
  if (obj != null)
  {
    if (obj is MemoryStream) //from Windows
    {
      var m = (obj as MemoryStream).ToArray();
      isCut = m[0] == 2;
    }
    else
      isCut = obj.ToString() == "Move";
  }

  PasteItems(data.GetFileDropList(), Path, isCut); //here start operation with items
}

private void ItemsToClipboard(StringCollection items, bool isCut)
{
  DataObject data = new DataObject();
  data.SetFileDropList(items);
  if (isCut)
    data.SetData(DROP_EFFECT, DragDropEffects.Move);
  else
    data.SetData(DROP_EFFECT, DragDropEffects.Copy);
  Clipboard.Clear();
  Clipboard.SetDataObject(data);
}