Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I drag files from my application to the Desktop? [duplicate]

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

like image 440
Neal Ehardt Avatar asked Oct 14 '25 14:10

Neal Ehardt


1 Answers

Try

if (File.Exists(filePath))
{
    string[] array = { filePath };
    var data = new DataObject(DataFormats.FileDrop, array);
    listBox1.DoDragDrop(data, DragDropEffects.Copy);
}
like image 55
Chuck Savage Avatar answered Oct 17 '25 04:10

Chuck Savage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!