Wednesday, June 20, 2012

Drag and drop files from WPF to desktop using C#.Net

This is not only applicable to WPF but also to Windows forms applications. When I meant desktop it applies to any folder opened in windows explorer. ie the folders which we interact with for drag and drop. The scenario here is simple. We have a remote file system accessible via WCF services and showing the files and folders in the front end WPF app. It has options to upload ,download,check-out, check in etc…As part of improving usability we decided to give drag and drop facility to the screens.

Some facts about .Net drag and drop
Here are the facts about .Net file drag and drop. We cannot manipulate the file contents during the drag operation. It works only with physical files. ie we should give the drag and drop system a physical file name to start drag and drop into windows explorer or desktop. May be one reason is once the dragging mouse leaves the .Net application it is controlled by the native system ie the windows.


The drag and drop design
The above facts tells us that we can only imitate a file drag start just like it is started from windows explorer. So we should design accordingly and it is simple. As long as the .Net file drag and drop works only with the files, we should download the file before we call the DoDragAndDrop method. Is it good to download when the user starts dragging? No.So give an indication that the file is being downloaded to the client side repository /cache /temp location and allow dragging on the files which are downloaded fully.
The attached sample contains code which puts a in-memory file entity to temp folder when the dragging starts and routes the source of dragging to the file which is created in temp.

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;
}



Clearing temp
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

4 comments:

  1. Hi, nice description about drag and drop files from wpf to desktop using c#.Thanks for your help..

    -Aparna
    Theosoft

    ReplyDelete
  2. above code is not working for me, when i drag and drop from wpf to any folder, file is not copying or moving, but same code works in winforms in windows10, if you have some idea on this please let me know

    ReplyDelete
  3. The code seems to only work for a Text file. I want to do the same for a .wav file. I changed public class FileEntry
    {
    public string FileName { get; set; }
    public byte[] FileContents { get; set; }
    }
    And
    private void btnAddToListBox_Click(object sender, RoutedEventArgs e)
    {
    string filePath = @"c:\Loops\Aminor\looperman-l-1319133-0116727-fanto8bc-premia-de-dalt.wav";
    string fileName = System.IO.Path.GetFileName(filePath);
    byte[] bytes = System.IO.File.ReadAllBytes(filePath);
    //string fileContents =
    files.Add(new FileEntry() { FileName = fileName, FileContents = bytes });
    }
    But the file just comes across an empty file

    ReplyDelete
  4. The casino at the Wynn - JTM Hub
    Get details on The Wynn, 광명 출장마사지 its 성남 출장샵 sister property, The Venetian, as well as the 양산 출장안마 Wynn Las 원주 출장안마 Vegas restaurants, bars, lounges, 나주 출장마사지 and boutiques,

    ReplyDelete