How to import multiple files from a smartphone in a WPF app?
We encounter a problem using
Microsoft.Win32.OpenFileDialog in a WPF app (targeting .NET Framework 4.7.2). I already asked the same question on stackoverflow.
We want to be able to import multiple files from a smartphone connected to a computer via USB connection. However, it seems impossible to do so using the
OpenFileDialog, because it fires message box "Cannot open multiple items from this location. Try selecting a single item instead.". You can reproduce it using this simple code and selecting at least 2 pictures from your smartphone:
var openFileDialog = new Microsoft.Win32.OpenFileDialog(); openFileDialog.Multiselect = true; openFileDialog.ShowDialog();
This problem is known (see here and here) and related to the MTP protocol. The problem does not occur with a smartphone connected using the alternative "USB Mas Storage" (UMS), but this option is not available on modern smartphones.
FileOpenPicker seems to handle better the selection of multiple items (see here to see how to use it in a WPF app) but we accounter an
InvalidCastException when using
PickMultipleFilesAsync. The problem is described here and the answers suggest that
FileOpenPicker cannot be used in WPF .NET Framework apps.
An other way to achieve this would be to implement a file picker from skratch, using the MTP protocol for the smartphone devices.
The downside of this approach is that the .NET and UWP file pickers offer a lots of great features and are fully integrated to the OS.
We have run out of ideas to solve this problem, so my question is:
Is there a way to import multiple files from a smartphone using the Windows file picker in a WPF app targeting .NET Framework?
I tested with win32 API with the thread GetOpenFileName for multiple files, it still can't select multiple from smartphone. In my view, there may be nomanaged API for accessing MTP. If you still want to try about this, you can try Portable Device APIs. There is a thread which describe this. If you want make a feature request for it, you can go to Developer Community to suggest a feature.
Thank you for your comment. That's what I feared. I know that some Microsoft desktop software are able to get around that limitation (Microsoft Outlook and Microsoft Word for example, but not Microsoft Edge), do you know if they use (and how) the UWP API?
To judge a UWP API can be used in the desktop app, need to check its Attributes contains
DualApiPartitionAttributeor not. If Attributes includes
DualApiPartitionAttribute(eg BluetoothAdapter Class ), this API can be used in desktop bridge app, there is no way to skip this.
Yes indeed, the FileOpenPicker does not have the
DualApiPartitionAttributeattribute (althought the
FileOpenPicker.PickSingpleFilesAsyncseems to be working from a desktop app). That's why I wondered how some Microsoft desktop apps are able to select multiple files from a MTP device.
I have created the suggestion : https://developercommunity.visualstudio.com/idea/1300797/allow-win32-apps-to-select-multiple-items-form-a-m.html
Do you know how many time it takes to have an update on an open suggestion?
I tested with IFileOpenDialog and I can select multiple files from my phone =>
IFileOpenDialog Test with Smartphone
@Castorix31 Thank you for your answer.
Are you sure that your phone is connected using the MTP protocol? I know that the multiple files selection works if the phone is connected using the UMS protocol, available on older versions of Android.
Which implementation of IFileOpenDialog are you using (I see that you seem to have customs buttons)?
Not sure for MTP. MTP is checked on my phone, and I just plugged it on a random USB port
I used an old code where I added a custom button with IFileDialogCustomize (which was for another question in MSDN forums), but not used here...
I investigated the issue a bit more, I found your post about the IFileDialogCustomize and tried to play with the Windows-classic-samples/Win7Samples/CommonFileDialogModes sample to access to all the FILEOPENDIALOGOPTIONS. Turns out that the FOS_FORCEFILESYSTEM is the one option which have impact on the files picked over the MTP protocol. As commented in the Forms.FileDialog_Vista (line 136), this flag make sure that the files picked are in the Windows file system. Using a MTP device, if this flag is enabled, a copy of the file is made on the Windows file system (temp folder). I guess it's disabled for multiple files to preserve disk space?
If this flag is disabled, the copy of the file is not made any more but it's possible to select multiple files. Also the IShellItem.GetDisplayName(SIGDN_FILESYSPATH) will fail (as the file is not in the Windows file system). One solution could be to handle this exception and download the file using the Media Devices APIs.
I did not use this flag (just FOS_ALLOWMULTISELECT) and it worked for me (with SIGDN_NORMALDISPLAY)
I could copy the files with IFileOperation from the returned IShellItemArray
Sign in to comment
I'm a bit late to reply but I found the solution thanks to @Castorix31 comments and samples.
As far as I know, it's not possible to select multiple files from a MTP device using the
Microsoft.Win32.OpenFileDialogbecause you can not set all the
As a result, you need to use your own
IFileOpenDialogto set the options: be sure to add the
FOS_ALLOWMULTISELECToption and remove the
You can find find the Window Shell API Interfaces on pinvoke.net:
Sign in to comment
0 additional answers
Sort by: Most helpful