UWP Portable device file copy Access is denied Exception

0

I am trying to read and copy files from a mobile phone to my PC. There is enough permission for the app but when I try to call the GetFilesAsync() function of Storage folder I get "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))". Following is the line of code I am using.

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

Are there other ways to fetch files from a portable device on UWP? Thanks in advance.

c#
uwp
windows-10-universal
asked on Stack Overflow Apr 9, 2020 by Aasish

1 Answer

0

UWP has strict management of file access rights, according to the description of removable storage in 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.

The application reports an error, it should be because you have no associated file type in the folder.

You can try to use FolderOpenPicker to select a folder in removable storage, this is a common practice:

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

User contributions licensed under CC BY-SA 3.0