C# UWP无法通过FolderPicker从硬盘访问文件夹

C# UWP无法通过FolderPicker从硬盘访问文件夹,c#,win-universal-app,uwp,windows-10-universal,file-access,C#,Win Universal App,Uwp,Windows 10 Universal,File Access,我想用UWP应用程序读取本地硬盘中某个文件夹(包括子文件夹)中的所有图像文件 我从FolderPicker开始,用户可以选择想要的文件夹: public async static Task<string> GetFolderPathFromTheUser() { FolderPicker folderPicker = new FolderPicker(); folderPicker.ViewMode = PickerViewMode.Thumb

我想用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异步静态任务GetFolderPathFromTheUser()
{
FolderPicker FolderPicker=新FolderPicker();
folderPicker.ViewMode=PickerViewMode.Thumbnail;
folderPicker.FileTypeFilter.Add(“.”);
var folder=await folderPicker.PickSingleFolderAsync();
返回文件夹路径;
}
成功获取文件夹路径后,我将尝试访问该文件夹:

 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;
    }
public异步静态任务IsContainImageFiles(字符串路径)
{
StorageFolder文件夹=等待StorageFolder.GetFolderFromPathAsync(路径);
IReadOnlyList temp=await folder.getfileasync();
foreach(临时存储文件sf)
{
如果(sf.ContentType==“jpg”)
返回true;
}
返回false;
}
然后我得到了下一个例外:

mscorlib.ni.dll中发生“System.UnauthorizedAccessException”类型的异常,但未在用户代码中处理 WinRT信息:无法访问指定的文件或文件夹(D:\test)。该项目不在应用程序有权访问的位置(包括应用程序数据文件夹、可通过功能访问的文件夹以及StorageApplicationPermissions列表中的持久化项目)。验证文件是否未标记系统或隐藏文件属性

那么我怎样才能从文件夹中读取文件呢


谢谢。

一旦从文件选择器中获取文件夹,您可能无法通过其路径访问该文件夹。您需要直接使用返回的StorageFolder实例:

public async static Task<IStorageFolder> GetFolderPathFromTheUser()
{
    FolderPicker folderPicker = new FolderPicker();
    folderPicker.ViewMode = PickerViewMode.Thumbnail;
    folderPicker.FileTypeFilter.Add(".");
    var folder = await folderPicker.PickSingleFolderAsync();
    return folder;
}

public async static Task<bool> IsContainImageFiles(IStorageFolder folder)
{
    IReadOnlyList<StorageFile> temp= await folder.GetFilesAsync();
    foreach (StorageFile sf in temp)   
    {
        if (sf.ContentType == "jpg")
            return true;
    }
    return false;
}
public异步静态任务GetFolderPathFromTheUser()
{
FolderPicker FolderPicker=新FolderPicker();
folderPicker.ViewMode=PickerViewMode.Thumbnail;
folderPicker.FileTypeFilter.Add(“.”);
var folder=await folderPicker.PickSingleFolderAsync();
返回文件夹;
}
公共异步静态任务IsContainImageFiles(IStorageFolder文件夹)
{
IReadOnlyList temp=await folder.getfileasync();
foreach(临时存储文件sf)
{
如果(sf.ContentType==“jpg”)
返回true;
}
返回false;
}
如果以后要访问它,应将其添加到并保留返回的令牌:

public async static Task<string> GetFolderPathFromTheUser()
{
    FolderPicker folderPicker = new FolderPicker();
    folderPicker.ViewMode = PickerViewMode.Thumbnail;
    folderPicker.FileTypeFilter.Add(".");
    var folder = await folderPicker.PickSingleFolderAsync();
    return FutureAccessList.Add(folder); 
}
public async static Task<bool> IsContainImageFiles(string accessToken)
{
    IStorageFolder folder = await FutureAccessList.GetFolderAsync(accessToken);
    IReadOnlyList<StorageFile> temp= await folder.GetFilesAsync();
    foreach (StorageFile sf in temp)   
    {
        if (sf.ContentType == "jpg")
            return true;
    }
    return false;
}
public异步静态任务GetFolderPathFromTheUser()
{
FolderPicker FolderPicker=新FolderPicker();
folderPicker.ViewMode=PickerViewMode.Thumbnail;
folderPicker.FileTypeFilter.Add(“.”);
var folder=await folderPicker.PickSingleFolderAsync();
返回FutureAccessList.Add(文件夹);
}
公共异步静态任务IsContainImageFiles(字符串accessToken)
{
IStorageFolder folder=Wait FutureAccessList.GetFolderAsync(accessToken);
IReadOnlyList temp=await folder.getfileasync();
foreach(临时存储文件sf)
{
如果(sf.ContentType==“jpg”)
返回true;
}
返回false;
}
那么,您是否“验证文件未标记系统或隐藏文件属性”?您是否验证了在给定应用程序的完整性级别和授权的情况下该路径是可访问的?相关信息:。