Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and drop to Desktop / Explorer

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.

like image 374
Dänu Avatar asked Jun 14 '10 19:06

Dänu


People also ask

How do I drag and drop in Windows Explorer?

In File Explorer, click any file or folder and hold the left button on your mouse. Then, press the Esc key. Now, try dragging and dropping again.

How do I drag something to my desktop?

Create shortcuts on your desktop by single click any icon or program file you want to create a shortcut of so it's highlighted. Once selected, click-and-hold the right mouse button, and drag that file to the desktop. When letting go of the mouse button, you have an option to Create a shortcut here.

Why can't I drag and drop icons on my desktop?

When drag and drop does not work, left-click a file in File Explorer and keep the left click mouse button pressed. While the left click button is held down, press the Escape key on your keyboard once. Then, release the left-click mouse button. Finally, try to drag and drop again.

How do I change drag and drop settings?

You can also use any of the keyboard shortcuts below to temporarily change the default drag and drop action for this instance. Press and hold the Ctrl key while you drag and drop to always copy. Press and hold the Shift key while you drag and drop to always move.


1 Answers

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.

like image 77
Ray Burns Avatar answered Sep 22 '22 01:09

Ray Burns