Reputation: 5276
I am working on a UWP application. I use the FolderPicker
to select a folder on the disk. Now what I want to do is search through the entire folder and pick video files and image files and show them in a slideshow.
Below is how I use the FolderPicker
to select a single folder.
FolderPicker openPicker = new FolderPicker()
{
ViewMode = PickerViewMode.Thumbnail,
SuggestedStartLocation = PickerLocationId.ComputerFolder
};
openPicker.FileTypeFilter.Add("*");
var SelectedFolder = await openPicker.PickSingleFolderAsync();
The problem I face is that event though I've picked the folder using the FolderPicker
when I select a single file as StorageFile
or a sub directory using a GetFolderFromPath()
it throws an UnAuthorizedAccessException
System.UnauthorizedAccessException: 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'
Below is how I access both of them:
StorageFolder accessFolder = StorageFolder.GetFolderFromPathAsync(SelectedFolder.Path + "\\subDir1\\subDir2").AsTask().GetAwaiter().GetResult(); // throws the exception
StorageFile file = accessFolder.GetFileAsync("DummyMediaFile.mp4").AsTask().GetAwaiter().GetResult(); // also throws the exception
Upvotes: 0
Views: 301
Reputation: 7727
There are two ways to achieve your purpose:
broadFileSystemAccess
capability.Please refer to the end of this document to add the broadFileSystemAccess
capability to the package.appxmanifest
file.
Looks like this:
<Package
...
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp uap5 rescap">
...
<Capabilities>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
Then find your application in Settings
-> Application
, click and select Advanced Settings
, open the File system
.
After that your code can be executed smoothly.
Although the path is a relatively simple way, in UWP, it is not allowed to directly access files or folders by path. You need to use the following method:
StorageFolder accessFolder = await (await SelectedFolder.GetFolderAsync("subDir1")).GetFolderAsync("subDir2");
StorageFile file = await accessFolder.GetFileAsync("DummyMediaFile.mp4");
Note, please use this method when confirming the above file or folder name is valid, otherwise please use
CreateFolderAsync("name", CreationCollisionOption.OpenIfExists)
Upvotes: 1