UWP app file picker exception: Access Denied to file

I'm trying to write a simple UWP app and I'm having trouble getting the picker to open a file. I've basically copied exactly as seen in the MSDN docs here.

When I try to open a file, I'm getting this error:

System.UnauthorizedAccessException
  HResult=0x80070005
  Message=Access to the path' 

C:\Users\user\Downloads\bgbtest.gb' is denied.
  Source=System.IO.FileSystem

I see in the documentation that capabilities should be set to access files outside of certain areas in a UWP application, but it also says:

In addition to the default locations, an app can access additional files and folders by declaring capabilities in the app manifest (see App capability declarations), or by calling a file picker to let the user pick files and folders for the app to access (see Open files and folders with a picker).

I'm only trying to read a file, not write or do anything fancy. Not sure where to go from here?

Posted Jul 19, 2019, 8:35 PM UTC

125
antiduh•

If I remember correctly, the picker object has a method to get a stream from the chosen file. Are you using that, or are you taking the name given to you and opening it by hand?

3
Archerofyail•

Can you post your relevant code?

Edit: As an example here's my method for restoring settings and other user data for my app. I am picking a whole folder, but the principle is the same

1
itsgreater9000•

IIRC you need to give permissions to your app to be able to open files that are not in the typical "allowed" locations... forget exactly what needs to be done though, check here: https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions#accessing-additional-locations

1
Archerofyail•

As long as you use the file or folder picker you will have permissions for the files/folders that the user picks.

2
itsgreater9000•

ah ok, probably what I remember doing was trying to just bring in a specific file instead of using the file picker. My bad! Thanks for the clarification

2