通过 FilePicker 访问时 StorageFolder.GetFolderFromPathAsync 出现 UnAuthorizedAccessException

UnAuthorizedAccessException on StorageFolder.GetFolderFromPathAsync while having access through FilePicker

我尝试从网络位置读取文件。但我不断收到 UnAuthorizedAccessException。

我通过 StorageFolder.GetFolderFromPathAsync 选择 StorageFolder,但列出文件会引发异常。

当我通过 FolderPicker 选择同一个文件夹时,它起作用了。

所以我尝试用这段代码找出问题所在:

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);
        }
    }
}

在设置变量pathFolderList的标记行抛出异常。虽然我已经在上面几行中列出了相同的文件夹。

我设置了这些功能:

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

我错过了什么?

您的应用无权访问该路径。访问文件的权限通过选取器返回的 StorageFolder 处理。

尝试从 pickedFolder 创建一个新的 StorageFolder 而不是这一行

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

只需使用 pickedFolder 本身:

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

我在我的博客文章中对此进行了更详细的介绍 Skip the path: stick to the StorageFile