UnAuthorizedAccessException on StorageFolder.GetFolderFromPathAsync while being accessed via FilePicker
I am trying to read files from a network location. But I keep getting UnAuthorizedAccessException.
I am selecting StorageFolder via StorageFolder.GetFolderFromPathAsync, but listing the files throws an exception.
When I select the same folder via FolderPicker, it works.
So I tried to identify 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);
}
}
}
An exception was thrown on the marked line where the pathFolderList parameter is set. Although I have 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?
source to share
Your application does not have access to the path. File permission is processed through the StorageFolder returned by the collector.
Instead of this line to try and create a new StorageFolder from selectedFolder
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(pickedFolder.Path);
Just use selectedFolder itself:
var pathFolderList = await pickedFolder.GetFilesAsync(); //Exception
I have covered this in detail in my blog post Skip the Path: Stick to StorageFile
source to share