WPF: Drag & Drop virtual folder from WPF to File Explorer

Alex SSB 20 Reputation points
2024-08-24T08:50:24.6533333+00:00

Hi, I am developing ADB Explorer, a UI for ADB in WPF.

I am trying to implement support for Drag & Drop to Push / Pull files between PC and a device connected via ADB, and while accepting files dragged into the app is quite easy since I can just use the provided path, its the other direction that I still can't get right. Namely, dragging a folder from the app to File Explorer.

I started by using VirtualFileDataObject class to transfer the files as streams, which is what's usually recommended for files that do not yet exist on the PC, but since ADB works best when transferring files instead of reading them as bytes, the solution is to pull the files into a temp folder, and send File Explorer the data from there.

Now although this works, this does require telling the shell all of the files to be transferred beforehand, which requires building a hierarchy tree before the drag begins, which in turn might take some time depending on the amount of files inside the selected folder. (FileGroupDescriptorW, FileContents)

I also tried using FileDrop together with InShellDragLoop which is used to tell the shell that not all of the required files are ready for transfer, but File Explorer didn't wait for me to set InShellDragLoop to FALSE and just copied (at best) the empty folder from the temp location.

So my question is what is the best approach here, and how can I make File Explorer wait for my transfer to my temp folder finish before taking the files from there?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,919 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,010 questions
{count} votes

1 answer

Sort by: Most helpful
  1. youzeliang 735 Reputation points
    2024-08-24T13:08:34.4266667+00:00

    To enable smooth Drag & Drop functionality from your WPF application to File Explorer, especially when dealing with virtual folders that need to be populated during the drag operation, you might consider using a combination of delayed rendering and asynchronous operations to manage the file transfer process.

    Here's a strategy that could work:

    1. Delayed File Enumeration:
    • Asynchronous File Enumeration: Start by preparing a list of files asynchronously as soon as the drag operation starts. Instead of blocking the UI, show a "Preparing files..." message or a progress indicator.
    • Manual Completion of DataObject: Use the FileGroupDescriptorW format to notify File Explorer about the files to be transferred, but instead of sending all the files upfront, send placeholders and manage the actual file generation or transfer asynchronously.
    1. Handling InShellDragLoop:
    • You can set InShellDragLoop to indicate that not all files are ready. However, ensure you do this correctly:
      • Implement a loop that monitors the status of the file transfer.
        • Update InShellDragLoop to FALSE only when the file transfer is complete and the files are available in the temp folder.
    • Use a dedicated background worker or task to handle the file transfer and update the shell as files become ready.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.