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.OpenFileDialog
because you can not set all the IFileOpenDialog
options.
As a result, you need to use your own IFileOpenDialog
to set the options: be sure to add the FOS_ALLOWMULTISELECT
option and remove the FOS_FORCEFILESYSTEM
:
ComFileDialog.IFileOpenDialog fileOpenDialog = null;
ComFileDialog.IShellItemArray shellItemArray = null;
ComFileDialog.IShellItem shellItem = null;
try
{
// Set options
fileOpenDialog = (ComFileDialog.IFileOpenDialog)new ComFileDialog.FileOpenDialogRCW();
fileOpenDialog.GetOptions(out var options);
options |= ComFileDialog.FOS_ALLOWMULTISELECT | ComFileDialog.FOS_FILEMUSTEXIST | ComFileDialog.FOS_PATHMUSTEXIST;
fileOpenDialog.SetOptions(options);
// Show window
if (fileOpenDialog.Show() != ComFileDialog.S_OK)
{
return;
}
// Get results
if (fileOpenDialog.GetResults(out shellItemArray) != ComFileDialog.S_OK)
{
return;
}
uint items = 0;
if (shellItemArray.GetCount(out items) != ComFileDialog.S_OK)
{
return;
}
// Result loop
for (uint item = 0; item < items; item++)
{
try
{
if (shellItemArray.GetItemAt(item, out shellItem) != ComFileDialog.S_OK)
{
continue;
}
// Use the IShellItem
}
finally
{
if (shellItem != null)
{
Marshal.ReleaseComObject(shellItem);
shellItem = null;
}
}
}
}
finally
{
if (fileOpenDialog != null)
{
Marshal.ReleaseComObject(fileOpenDialog);
}
if (shellItemArray != null)
{
Marshal.ReleaseComObject(shellItemArray);
}
if (shellItem != null)
{
Marshal.ReleaseComObject(shellItem);
}
}
You can find find the Window Shell API Interfaces on pinvoke.net:
- https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ifiledialog
- https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ifileopendialog
- http://pinvoke.net/default.aspx/Interfaces/IFileOpenDialog.html
- https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ishellitem
- http://pinvoke.net/default.aspx/Interfaces/IShellItem.html
- https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ishellitemarray
- http://pinvoke.net/default.aspx/Interfaces/IShellItemArray.html
- http://www.pinvoke.net/default.aspx/Interfaces/FileOpenDialogRCW.html