Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP FolderPicker.PickSingleFolderAsync fails with COMException / E_FAIL

Tags:

c#

uwp

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.

like image 781
Stuart Whitehouse Avatar asked Mar 08 '18 17:03

Stuart Whitehouse


1 Answers

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.

like image 55
Martin Zikmund Avatar answered Oct 11 '22 09:10

Martin Zikmund