user8296390
user8296390

Reputation: 23

Get all files inside a specific folder in a library with UWP

I'm trying to get all the videos in a specific folder inside the Videos library using UWP, right now I can get all videos inside the Videos library, but I'd like to reduce my results to only those inside the specified folder. My code is this:

            Windows.Storage.Search.QueryOptions queryOption = new QueryOptions(CommonFileQuery.OrderByTitle, new string[] {".mp4"});

            queryOption.FolderDepth = FolderDepth.Deep; 

            var files = await KnownFolders.VideosLibrary.CreateFileQueryWithOptions(queryOption).GetFilesAsync();
            StorageFile videoToPlay = (files[new Random().Next(0, files.Count)] as StorageFile);
            var stream = await videoToPlay.OpenAsync(Windows.Storage.FileAccessMode.Read);
            Player.SetSource(stream, videoToPlay.ContentType);
            Debug.WriteLine(Player.Source);

How could I access a subfolder named "Videos to Play" and then get all the videos inside that folder? I tried accesing it by using a path like:

        string localfolder = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
        var array = localfolder.Split('\\');
        var username = array[2];
        string[] allVideos = System.IO.Directory.GetFiles("C:/Users/" + username + "/Videos/Videos to Play");

But I get access denied even though I already requested access to the Videos library (and the fact that the first example works shows that I actually have access to it).

Upvotes: 1

Views: 1214

Answers (1)

Péter Bozsó
Péter Bozsó

Reputation: 1328

try
{
    var folder = await KnownFolders.VideosLibrary.GetFolderAsync("Videos to Play");
}
catch (FileNotFoundException exc)
{
    // TODO: Handle the case when the folder wasn't found on the user's machine.
}

In the folder variable you'll have the reference to the desired folder. Then it's the very same stuff that you already do, but instead of KnownFolders.VideosLibrary folder use this one!

Upvotes: 2

Related Questions