question

Max-9405 avatar image
0 Votes"
Max-9405 asked ·

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.

The UWP `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?

dotnet-csharpwindows-wpf
· 11
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tested with IFileOpenDialog and I can select multiple files from my phone =>

IFileOpenDialog Test with Smartphone


1 Vote 1 ·

@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)?

0 Votes 0 ·

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...

0 Votes 0 ·
Show more comments

@Max-9405
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.


0 Votes 0 ·

@DaisyTian-MSFT
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?

0 Votes 0 ·

@Max-9405
To judge a UWP API can be used in the desktop app, need to check its Attributes contains DualApiPartitionAttribute or not. If Attributes includes DualApiPartitionAttribute (eg BluetoothAdapter Class ), this API can be used in desktop bridge app, there is no way to skip this.


0 Votes 0 ·
Show more comments

0 Answers