mjw
mjw

Reputation: 410

GetFolderFromPathAsync function access denied

I'm making a Windows 10 Universal App and I want the user to pick a folder to save the document files for the App. The path for this folder is saved to ApplicationData.Current.RoamingSettings.Values. Here's the code:

On first Start:

var folderPicker = new FolderPicker { SuggestedStartLocation = PickerLocationId.ComputerFolder };
        StorageFolder folder = await folderPicker.PickSingleFolderAsync();
        StorageFolder homeFolder = await folder.CreateFolderAsync("App1 Data", CreationCollisionOption.OpenIfExists);

        var save = ApplicationData.Current.RoamingSettings.Values;
        save["HomeFolder"] = homeFolder.Path;

When HomeFolder is set:

string dir = save["HomeFolder"].ToString();
        try
        {
            StorageFolder homeFolder = await StorageFolder.GetFolderFromPathAsync(dir);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
        }

The thrown Exception in the second code sample is:

System.UnauthorizedAccessException: access denied (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

So my question is, how do you use the GetFolderFromPathAsync function correctly? I checked all strings for the paths, they are all right, even

StorageFolder.GetFolderFromPathAsync(storageFolder.Path);

doesn't work. Do you know a solution?

Upvotes: 3

Views: 3086

Answers (1)

Rob Caplan - MSFT
Rob Caplan - MSFT

Reputation: 21899

Use the StorageFile directly rather than converting to a path.

To store the file returned from the file picker for later use save the StorageFile in the AccessCache classes FutureAccessList or MostRecentlyUsedList. The path doesn't include the spermissions needed to open the file. The StorageFile carries the permissions and grants access to the file.

I discussed this in more detail in my blog entry Skip the path: stick to the StorageFile

Upvotes: 3

Related Questions