Access all files in folder from FileActivated file

2019-07-17 02:05发布

问题:

I write a Windows 10 Universal App like the default Photo App in C#. The Photo App allows to show all images in the directory where the user opened the file with the Photo App. But when the user opened a file with my App I get only the FileActivatedEventArgs with allows to display the file the user opened. I found no solution to show the user the other files from the directory of this file too. I think the problem is to get the permission to access that files because when the folder of the file is in the picture library it works. But the Windows Photo App can this everywhere so I think there must be a solution ...

Edit: I have tried to extract a very simple sample code from my project that show the relevant part in only a few lines of code

public static async Task<BitmapImage> LoadImage(StorageFile file)
{
    BitmapImage bitmapImage = new BitmapImage();
    FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);
    bitmapImage.SetSource(stream);
    return bitmapImage;
}

private async void setImages(FileActivatedEventArgs args)
{
    StorageFile si = (StorageFile)App.args.Files.First();
    StorageFolder st = await si.GetParentAsync();
    StorageApplicationPermissions.FutureAccessList.Add(st);
    IReadOnlyList<StorageFile> sflist = await st.GetFilesAsync();
    foreach (StorageFile sf in sflist)
    {
        imageList.Add(await LoadImage(sf));
    }
}

回答1:

Yes, you have NO access rights for the folder. In this case, GetParentAsync() may fall. You should use "IFileActivatedEventArgsWithNeighboringFiles" to parse the neighboring files. By using this interface, OS's File Broker process passed the neighboring files to you.

https://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.activation.ifileactivatedeventargswithneighboringfiles

Note - This API was added from Win8.1. And, Win8.0 ver of Photo app can't show the neighboring files when it was activated by FileActivated. Win8.1 ver of Photo app may use this API.



回答2:

You may need to add the following to the Package.appxmanifest file so you can access the Pictures Library then you should be able to get this to work:

<Capabilities> 
   <uap:Capability Name="picturesLibrary"/> 
</Capabilities>