3
votes

I want to read all images files from some folder in the local hard drive (including sub folders) with a UWP app.

im starting with FolderPicker so the user can pick the wanted folder:

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;
    }

after successfully getting the folder path i'm trying to access the folder:

 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;
    }

and then I'm getting the next exception :

An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.ni.dll but was not handled in user code WinRT information: Cannot access the specified file or folder (D:\test). The item is not in a location that the application has access to (including application data folders, folders that are accessible via capabilities, and persisted items in the StorageApplicationPermissions lists). Verify that the file is not marked with system or hidden file attributes.

So how can I get access to read the files from the folder?

Thanks.

1
So, did you "verify that the file is not marked with system or hidden file attributes"? Did you verify, that the path is accessible given your application's integrity level and authorization?IInspectable
Relevant information: File access permissions.IInspectable

1 Answers

4
votes

Once you get a folder from the file picker, you may not be able to access the folder through its path. You need to directly use the StorageFolder instance returned:

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;
}

If you want to access it later, you should add it to future access list and keep the returned token:

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;
}