Drag and drop to Desktop / Explorer
Asked Answered
C

1

32

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.

Canute answered 14/6, 2010 at 19:53 Comment(2)
it would be appreciated if you posted your complete solution as an answer here.Amorita
I tried your example but it wont compile, DragDrop is unknown. Do I need to add a reference or something ?Reynaud
L
30

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.

Litter answered 15/6, 2010 at 8:3 Comment(7)
I know this is a very old thread, but could you expand on your statement about "unless your files are potentially very large"-part? I'm trying to do exactly that. Have a DragDrop functionality, which downloads the file when dropping it somewhere.Colonic
How to do the same with DragAndDrop folders?Legitimacy
@Legitimacy Same as files. Folder are treated as Files.Prelate
@Ray-Burns Copying a large file to temp location might take a lot of time. Is there any other way to achieve this and resolve IDataObject interactions at the same time? Please have a look at my Question Here also.Prelate
@AishwaryaShiva but they are not copied to explorer as files. I do DragDrop and nothing happens.Legitimacy
@Ray-Burns where does the DragDrop object comes from ? I tried your example but DragDrop is not a know class. Do I need to add a reference ?Reynaud
It should be noted that having the data in an array is crucial even if you have only one file!Sheepshead

© 2022 - 2024 — McMap. All rights reserved.