Monday, December 15, 2008

I spent a while working this one out and none of the google hits  showed me what I needed so I thought I'd blog it here. When you hook the Drop event how do you deal with multiple files being dropped? The GetData("FileNameW") member only returns a single file name in the string array even if multiple files are being dragged. GetData("FileName") returns a single file name in 8.3 compatible munged form so that doesn't help. I looked in the help for System.Windows.DataObject and it has a GetFileDropList member. However, it turns out that the DragDropEventArgs.Data is an IDataObject not a DataObject. So if you cast it to a DataObject you get access to the file list

if (e.Data.GetFormats().Contains("FileDrop"))
{
    DataObject dataObject = e.Data as DataObject;
    StringCollection files = null;
    if (dataObject != null)
    {
        files = dataObject.GetFileDropList();
    }
    else
    {
        // not sure if this is necessary but its defensive coding
        string[] fileArray = (string[])e.Data.GetData("FileNameW");
        files = new StringCollection();
        files.Add(fileArray[0]);
    }

    foreach (string file in files)
    {
        // process files
    }
}

Hopefully someone else looking how to do this will stumble across this blog post and save some time

 | 
Monday, December 15, 2008 8:01:53 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]Trackback
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):