UWP 便携式设备文件复制访问被拒绝异常

UWP Portable device file copy Access is denied Exception

我正在尝试从移动设备 phone 读取文件并将其复制到我的电脑上。该应用程序有足够的权限,但是当我尝试调用 Storage 文件夹的 GetFilesAsync() 函数时,我得到 "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"。以下是我正在使用的代码行。

            StorageFolder UsbDrive = (await Windows.Storage.KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault();  //StorageFolder object that maps all removable devices as subfolders.

            var rootFolders = await UsbDrive.GetFoldersAsync();

            foreach (var itemRootFolder in rootFolders)
            {
                var allFolders = await itemRootFolder.GetFoldersAsync();
                foreach (var itemAllFolder in allFolders)
                {
                    Console.WriteLine("DisplayName:  " + itemAllFolder.DisplayName + "DateCreated:  " + itemAllFolder.DateCreated + "DisplayType:  " + itemAllFolder.DisplayType + "FolderRelativeId:  " + itemAllFolder.FolderRelativeId);
                    var myNeedFolders = await itemAllFolder.GetFoldersAsync();
                    foreach (var myNeedFoder in myNeedFolders)
                    {
                        IReadOnlyList<StorageFile> FileList = await myNeedFoder.GetFilesAsync();

                        System.Diagnostics.Debug.WriteLine("LISTING FILES:");
                        foreach (StorageFile File in FileList)
                            System.Diagnostics.Debug.WriteLine(File.Name);
                    }
                }
            }

是否有其他方法可以在 UWP 上从便携式设备获取文件?提前致谢。

UWP对文件访问权限有严格的管理,根据this document中可移动存储的描述:

The removableStorage capability ... filtered to the file-type associations declared in the package manifest. For example, if a document-reader app declares a .doc file-type association, it can open .doc files on the removable storage device, but not other types of files.

应用程序报错,应该是你的文件夹中没有关联的文件类型。

您可以尝试使用FolderOpenPicker到select可移动存储中的文件夹,这是一种常见的做法:

var folderPicker = new FolderPicker()
{
    SuggestedStartLocation = PickerLocationId.ComputerFolder
};
folderPicker.FileTypeFilter.Add("*");
var folder = await folderPicker.PickSingleFolderAsync();
// do other things...