The drag and drop design
When should I call DoDragDrop
I had tried from so many places and succeeded when I called from PreviewLeftMouseButtonDown. This will block all the other operations on the mouse down operation context.
Code to drag and drop to explorer
If you look at the same you will get a clear idea how I achieved this .The FileEntry is just a class to hold the data and file name. Upon mouse event I wrote to a temp location and starts dragging using that file path. ie simply initiating a drag which we do among normal windows explorer windows.
private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { FrameworkElement senderElement = (sender as FrameworkElement); if (senderElement != null) { FileEntry file = senderElement.DataContext as FileEntry; if (file != null) { string fullPath = Write2TempAndGetFullPath(file); //TODO : If you want to delete you need to use FileWatchers to catch the drop events and delete from temp. //But as long as it is temp the users/admins can delete it when required. DataObject dragObj = new DataObject(); dragObj.SetFileDropList(new System.Collections.Specialized.StringCollection() { fullPath }); DragDrop.DoDragDrop(senderElement, dragObj, DragDropEffects.Copy); } } } private string Write2TempAndGetFullPath(FileEntry file) { string tempFilePath=System.IO.Path.Combine(_tempFolder,file.FileName); Stream fs=File.Create(tempFilePath); new StreamWriter(fs).WriteLine(file.FileContents); fs.Close(); return tempFilePath; }
If you respect the word TEMP ,there is no need to worry about clearing the temp folder because that is temporary and users or admins are allowed or trained to clean that folder whenever they need more hard disk space . If you agree with me go ahead and concentrate on your application and leave the temp to users. Else ie if you are particular about cleaning temp follow the below link which employees a file watcher to clean the temp immediately after the file is dropped.
http://www.codeproject.com/Articles/23207/Drag-and-Drop-to-Windows-Folder-C
Sample
Download the sample from this link