【发布时间】:2016-06-12 15:32:15
【问题描述】:
我想使用 UWP 应用从本地硬盘驱动器中的某个文件夹(包括子文件夹)中读取所有图像文件。
我从 FolderPicker 开始,所以用户可以选择想要的文件夹:
public async static Task<string> GetFolderPathFromTheUser()
{
FolderPicker folderPicker = new FolderPicker();
folderPicker.ViewMode = PickerViewMode.Thumbnail;
folderPicker.FileTypeFilter.Add(".");
var folder = await folderPicker.PickSingleFolderAsync();
return folder.Path;
}
成功获取文件夹路径后,我正在尝试访问该文件夹:
public async static Task<bool> IsContainImageFiles(string path)
{
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(path);
IReadOnlyList<StorageFile> temp= await folder.GetFilesAsync();
foreach (StorageFile sf in temp)
{
if (sf.ContentType == "jpg")
return true;
}
return false;
}
然后我得到下一个异常:
“System.UnauthorizedAccessException”类型的异常发生在 mscorlib.ni.dll 中,但未在用户代码中处理 WinRT 信息:无法访问指定的文件或文件夹 (D:\test)。该项目不在应用程序有权访问的位置(包括应用程序数据文件夹、可通过功能访问的文件夹以及 StorageApplicationPermissions 列表中的持久项目)。确认该文件未标记系统或隐藏文件属性。
那么我怎样才能获得从文件夹中读取文件的权限呢?
谢谢。
【问题讨论】:
-
那么,您是否“验证该文件没有标记系统或隐藏文件属性”?您是否验证了在您的应用程序的完整性级别和授权下,该路径是可访问的?
-
相关信息:File access permissions.
标签: c# win-universal-app uwp windows-10-universal file-access