UWP FolderPicker.PickSingleFolderAsync fails with COMException / E_FAIL

4

In my UWP app I have the following code:

private async void testButton_Click(object sender, RoutedEventArgs e)
{
  var picker = new Windows.Storage.Pickers.FolderPicker();
  StorageFolder folder = await picker.PickSingleFolderAsync();
}

but when I run this it fails on the second line with the message An exception of type 'System.Runtime.InteropServices.COMException' occurred in .... but was not handled in user code. The HRESULT from the exception is -2147467259 = 0x80004005 = E_FAIL.

I'm using file pickers already elsewhere in the app without problem. This is running on a Win10 desktop (launched from VS2015). Can anyone suggest why the error occurs and/or what to do to resolve it? Having a meaningless error message in what appears to be the simplest code possible I'm not sure how to proceed.

c#
uwp

1 Answer

10

This is a bit of an oddity in WinRT. Although it is not mentioned in the documentation explicitly, it is necessary to add at least one item in the FileTypeFilter collection:

var folderPicker = new FolderPicker();
folderPicker.FileTypeFilter.Add("*");
await folderPicker.PickSingleFolderAsync();

You can use a specific extension like ".jpg", but it doesn't seem to have effect in a FolderPicker anyway. The only thing that matters is that at least one valid item is present.

answered on Stack Overflow Mar 8, 2018 by Martin Zikmund

User contributions licensed under CC BY-SA 3.0