【问题标题】:UWP Portable device file copy Access is denied ExceptionUWP 便携式设备文件复制访问被拒绝异常
【发布时间】:2020-07-22 00:15:48
【问题描述】:

我正在尝试从手机读取文件并将其复制到我的 PC。该应用程序有足够的权限,但是当我尝试调用 Storage 文件夹的 GetFilesAsync() 函数时,我得到 “访问被拒绝。(来自 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 上从便携式设备获取文件吗?提前致谢。

【问题讨论】:

    标签: c# uwp windows-10-universal


    【解决方案1】:

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

    removableStorage 功能...过滤到包清单中声明的​​文件类型关联。例如,如果文档阅读器应用声明了 .doc 文件类型关联,则它可以打开可移动存储设备上的 .doc 文件,但不能打开其他类型的文件。

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

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

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

    【讨论】:

    • 嗨,理查德,非常感谢您的回答。但这不是我想要的。我想复制文件而不选择或浏览。仍然无法弄清楚如何实现或不可能。
    • 对不起,这不能在 UWP 中完成。 UWP 应用程序只有在访问自己的应用程序文件夹时才有完全权限,访问其他文件夹需要用户授权。对于可移动设备上的文件访问,需要提前声明关联的文件类型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 2023-03-10
    • 2013-09-30
    相关资源
    最近更新 更多