Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetFolderFromPathAsync function access denied

Tags:

c#

uwp

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?

like image 728
mjw Avatar asked Sep 01 '26 10:09

mjw


1 Answers

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

like image 73
Rob Caplan - MSFT Avatar answered Sep 02 '26 23:09

Rob Caplan - MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!