GetFolderFromPathAsync function access denied

不羁岁月 提交于 2019-12-07 22:29:02

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/33200332/getfolderfrompathasync-function-access-denied

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!