File picker multi select failed with selection of a lot of files.

Dani_S 4,501 Reputation points
2023-05-09T12:36:37.1266667+00:00

Hi,

When i select 1000 files with multi select the app crash with this exception:

The maximum number of items for the access list has been reached. An item must be removed before another item is added. (0x80270220)

   PickOptions options = new()
   {
       //PickerTitle = "Please select files",
   };
   
   var fileResults = await _filePicker.PickMultipleAsync(options);

   public async Task<IEnumerable<FileResult>> PickMultipleAsync(PickOptions options)
   {
       try
       {
           var result = await FilePicker.Default.PickMultipleAsync(options);
           if (result != null)
           {
               return result;
           }
        }
        catch (Exception ex)
        {
           // The user canceled or something went wrong
        }

        return null;
   }
Developer technologies | .NET | .NET MAUI
{count} votes

Accepted answer
  1. Rob Caplan - MSFT 6,037 Reputation points Microsoft Employee Moderator
    2023-05-09T23:30:57.91+00:00

    Assuming from the error that you're running in a packaged Windows app, the error is correct and descriptive. The FilePicker tries to add all picked files to the FutureAccessList, and the FutureAccessList can hold only 10000 items.

    That said, there's no general need to add items to the FutureAccessList in a WinUI3 app as there was for a UWP app, so that's an unnecessary check. You can work around this by calling the Windows.Storage.Pickers.FileOpenPicker directly from platform specific code instead of calling the MAUI FilePicker. Here's a code snippet from the FileOpenPicker docs modified to initialize in a WinUI3 context and to demonstrate that the file can be opened from its Path, which doesn't carry the permissions granted by the FileOpenPicker.

    #if WINDOWS
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".png");
    
            // HWND initialization needed for WinUI3
            var hwnd = WindowStateManager.Default.GetActiveWindow().GetWindowHandle();
            WinRT.Interop.InitializeWithWindow.Initialize(openPicker, hwnd);
    
            var files = await openPicker.PickMultipleFilesAsync();
            if (files != null)
            {
                foreach (var file in files)
                {
                    // WinUI3 has full trust by default and so can open the Path with System.IO.File 
                    // UWP would need to access this via the StorageFile
                    using (FileStream f = System.IO.File.OpenRead(file.Path))
                    {
                        Debug.WriteLine($"Picked photo: {file.Name} is {f.Length}");
                    }
                }
            }
            else
            {
                Debug.WriteLine($"Operation cancelled.");
            }
    #endif
    

0 additional answers

Sort by: Most helpful

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.