简体   繁体   中英

Drag and Drop from Outlook into Winforms

When dragging items from Outlook email into a Winforms app (Control is a GalleryControl by DevExpress, the DragDrop event is not firing, even though i manually set 'DragDropEffects.Move` in the DragEnter event handler. (have confirmed that this is firing)

However DragDrop event does fire just when dragging normal files from windows explorer.

    private async void gcImages_DragDrop(object sender, DragEventArgs e)
    {

        string[] fileNames = null;

        if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
        {
            fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
        }
        else if (e.Data.GetDataPresent("FileGroupDescriptor"))
        {
            OutlookDataObject dataObject = new OutlookDataObject(e.Data);
            string[] filenames = (string[])dataObject.GetData("FileGroupDescriptor");
        }
        // do stuff async with file names
    }

    private void gcImages_DragEnter(object sender, DragEventArgs e)
    {
        // This event fires, no matter what i drag onto it.  (Files from explorer, attachments from Outlook etc)  
        // However even after setting the effect as per below, the cursor still shows the 'not allowed' symbol.
        e.Effect = DragDropEffects.Move;
    }

I have enabled AllowDrop = true on the control, and it works perfectly with Windows Explorer files, just not outlook files.

The strange thing is that the DragEnter event is firing, but the DragDrop event does not fire with Outlook attachments.

Ended up using this code, seemed to work fine.

//// Use Like This

    private void gcImages_DragDrop(object sender, DragEventArgs e)
    {
        DragDropHelper.AcceptDroppedFile(e, AddAndSaveNewDocument);
    }

    private void AddAndSaveNewDocument(FileSystemInfo fileInfo)
    {

    }


////


public static class DragDropHelper
{
    public static void AcceptDroppedFile(DragEventArgs e, Action<FileInfo> addAndSaveNewDocument)
    {
        string[] fileNames = null;

        if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
        {
            fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
        }
        else if (e.Data.GetDataPresent("FileGroupDescriptor"))
        {
            var dataObject = new OutlookDataObject(e.Data);
            fileNames = (string[])dataObject.GetData("FileGroupDescriptor");

            for (var i = 0; i < fileNames.Length; i++)
            {
                var itm = fileNames[i];
                using var ms = dataObject.GetData("FileContents", i);

                var tmpFileName = Path.Combine(Path.GetTempPath(), itm);

                using (var file = new FileStream(tmpFileName, FileMode.Create, System.IO.FileAccess.Write))
                {
                    byte[] bytes = new byte[ms.Length];
                    ms.Read(bytes, 0, (int)ms.Length);
                    file.Write(bytes, 0, bytes.Length);
                    ms.Close();
                }

                fileNames[i] = tmpFileName;
            }
        }
        if (fileNames != null)
        {
            foreach (var fileName in fileNames)
            {
                var fileInfo = new FileInfo(fileName);

                addAndSaveNewDocument(fileInfo);

                if (fileName.Contains(Path.GetTempPath(), StringComparison.CurrentCultureIgnoreCase))
                {
                    File.Delete(fileName);
                }
            }
        }
    }
}

Code here for OutlookDataObject class https://codeshare.io/G7747D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM