Search code examples
c#windows-phone-8.1known-folders

Get music library files


Trying to excess Music folder on windows phone app 8.1 but getting following Exception:

System.Reflection.TargetInvocationException was unhandled Message: An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll Additional information: Exception has been thrown by the target of an invocation.

Here is the code: Calling

try
{
    GetFiles();
}
catch (Exception ex)
{
    Debug.WriteLine(ex.Message);
}

private async void GetFiles()
{
    StorageFolder folder = KnownFolders.MusicLibrary;
    IReadOnlyList<StorageFile> listOfFiles;
    if (folder!=null)
    {
        listOfFiles =await folder.GetFilesAsync(); //this line casuing Debugger.break();
    }
}

Solution

  • Add Music-library capability in package.appmanifest file

    Then try this code. It worked for me

        StorageFolder folder = KnownFolders.MusicLibrary;
        if (folder!=null)
        {
           var songs = (await   folder.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName)).ToList();
        }
    

    Link to Sample Code