Get file (PDF/JPEG...) and convert to byte array. (0x80070005)

Salavat 121 Reputation points
2022-09-29T19:29:02.667+00:00

Hello.
Encountered an error when using the following code:

private async void DB_Add_Drawing(object sender, RoutedEventArgs e)  
        {  
            try  
            {  
                ...  
                var folderPicker = new FileOpenPicker();  
                folderPicker.CommitButtonText = "Select a file!";  
                folderPicker.ViewMode = PickerViewMode.Thumbnail;  
                folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;  
                folderPicker.FileTypeFilter.Add(".pdf");  
                folderPicker.FileTypeFilter.Add(".jpeg");  
                folderPicker.FileTypeFilter.Add(".png");  
  
                StorageFile storageFile = await folderPicker.PickSingleFileAsync();  
                string Path = storageFile.Path;  
  
                StorageFile storageFile1 = await StorageFile.GetFileFromPathAsync(Path);  
  
                byte[] drawByte = await storageFile1.ReadBytesAsync();  
                ...  
            }  
              
            catch(Exception ex)  
            {  
               ...  
            }  
        }  

This handler was created to get a file and write the path of the selected file to the SQL cell and in parallel write the file itself through transformations into a byte array to the next cell. But at the stage StorageFile storageFile1 = await StorageFile.GetFileFromPathAsync(Path); I'm getting the error [ System.UnauthorizedAccessException: 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))' ]. But why does this error occur if the file is selected via FileOpenPicker()? Is it necessary to declare Enterprise Authentication in the manifest? The selected file can be located anywhere on the disk (documents, desktop, and so on), as well as in a network folder.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 49,246 Reputation points
    2022-09-29T20:33:11.747+00:00

    You already have the StorageFile. Trying to get the file again using GetFileFromPathAsync doesn't make sense to me. You already have it. I suspect that is the problem.

    If the user navigates to their Documents directory within the UI then you don't need permissions to access it. But if you do so programmatically, like you're doing here, then you would need access anywhere they referenced. From my understanding of how UWP permissions work.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful