GetFolderFromPathAsync 函数访问被拒绝 [英] GetFolderFromPathAsync function access denied

查看:36
本文介绍了GetFolderFromPathAsync 函数访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 Windows 10 通用应用程序,我希望用户选择一个文件夹来保存该应用程序的文档文件.此文件夹的路径保存到 ApplicationData.Current.RoamingSettings.Values.代码如下:

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:

第一次启动:

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;

设置 HomeFolder 时:

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))

所以我的问题是,您如何正确使用 GetFolderFromPathAsync 函数?我检查了路径的所有字符串,它们都没问题,甚至

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?

推荐答案

直接使用 StorageFile 而不是转换为路径.

Use the StorageFile directly rather than converting to a path.

要存储从文件选择器返回的文件供以后使用,请将 StorageFile 保存在 AccessCache 类 FutureAccessList 或 MostRecentlyUsedList 中.该路径不包括打开文件所需的权限.StorageFile 携带权限并授予对文件的访问权限.

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

这篇关于GetFolderFromPathAsync 函数访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆