Drag and drop to Desktop / Explorer

Dänu picture Dänu · Jun 14, 2010 · Viewed 13.6k times · Source

Following my scenario.

I got an Application which loads a Filestructure (Folders, Files) from a Database into a WPF ListView. Now I'd like to grab a file from this ListView, drag it over my Desktop (or some open explorer window) and drop it there. Basic Drag and Drop, nothing fancy. This sounds like a "standard" function for a windows application - but google won't help.

So how can I achieve this? Interops?

Thanks

Edit: Thanks for the solution, I still had to do some googling. Here's my complete solution.

Answer

Ray Burns picture Ray Burns · Jun 15, 2010

DragDrop.DoDragDrop can do this as long as you pass it an appropriate DataObject.

First copy the files somewhere. You can use System.IO.Path.GetTempPath() if you don't have anywhere better.

Next create a string array containing the full paths to the files and do the following:

string[] paths = ...;
DragDrop.DoDragDrop(this, new DataObject(DataFormats.FileDrop, paths),
                    DragDropEffects.Copy); 

It is actually possible to do this without pre-copying the files but that gets into some complicated IDataObject interactions, so unless your files are potentially very large and aren't already in the filesystem I would try this method first.