Arjan Snoek
Arjan Snoek

Reputation: 103

UnAuthorizedAccessException on StorageFolder.GetFolderFromPathAsync while having access through FilePicker

I try to read files from a network location. But I keep getting an UnAuthorizedAccessException.

I pick the StorageFolder through StorageFolder.GetFolderFromPathAsync but listing the files throws the exception.

When I pick the same folder through the FolderPicker it works.

So I tried pinpointing the problem with this code:

FolderPicker picker = new FolderPicker();
picker.FileTypeFilter.Add("*");

StorageFolder pickedFolder = await picker.PickSingleFolderAsync();
if (pickedFolder != null)
{
    var pickedFolderList = await pickedFolder.GetFilesAsync();
    var count = pickedFolderList.Count;
    if (count > 0)
    {
        StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(pickedFolder.Path);
        var pathFolderList = await folder.GetFilesAsync(); //Exception
        if (pathFolderList.Count == count)
        {
             ProcessFolder(folder);
        }
    }
}

The exception is thrown at the marked line where the variable pathFolderList is set. While I had already listed the same folder a few lines above.

I have set these capabilities:

<Capabilities>
  <Capability Name="internetClient" />
  <Capability Name="privateNetworkClientServer"/>
  <uap:Capability Name="enterpriseAuthentication"/>
  <uap:Capability Name="removableStorage"/>
</Capabilities>

What am I missing?

Upvotes: 4

Views: 1748

Answers (1)

Rob Caplan - MSFT
Rob Caplan - MSFT

Reputation: 21899

Your app doesn't have access to the path. The permission to access the file is handled via the StorageFolder returned by the picker.

Instead of this line to try to create a new StorageFolder from pickedFolder

StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(pickedFolder.Path);

Just use the pickedFolder itself:

var pathFolderList = await pickedFolder.GetFilesAsync(); //Exception

I went into this in more detail in my blog entry Skip the path: stick to the StorageFile

Upvotes: 3

Related Questions