3

Possible Duplicate:
C# / WPF / .NET - Drag and drop to Desktop / Explorer

I've managed to drag files from the Desktop into my WPF application. Now I want to drag them back out.

This code does not work. Everything looks right (the cursor turns into a +) but the file is not copied.

listBoxItem.PreviewMouseLeftButtonDown += (o, e) =>
{
    Console.WriteLine("drag leave");

    // changing this line to: var data = "a string"; works for text dragging
    var data = new DataObject(DataFormats.FileDrop, filePath);

    // also tried DragDropEffects.Copy with no success
    DragDrop.DoDragDrop(item, data, DragDropEffects.All);
};

Any ideas?

Very similar question here but I don't understand their answer: c# drag drop DataObject

Thanks,

Neal

Community
  • 1
  • 1
Neal Ehardt
  • 10,334
  • 9
  • 41
  • 51

1 Answers1

3

Try

if (File.Exists(filePath))
{
    string[] array = { filePath };
    var data = new DataObject(DataFormats.FileDrop, array);
    listBox1.DoDragDrop(data, DragDropEffects.Copy);
}
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69