How to get files from the Windows music library (8.1) using path names?

I allow my users to select files from the music library. After that, I save the path to the selected files locally. Now my problem is how do I access files programmatically from these paths? Here are a few things I've tried.

Say my path is D: \ Xyz \ abc.mp3

StorageFolder S = KnownFolders.MusicLibrary;

Alternative 1:

StorageFile MyFile = waiting for S.GetFileAsync ("abc.mp3"); // File Error Not Found

// I believe this does not work because the GetFileAsync method can only receive files from the current folder

Alternative 2:

S = wait S.GetFolderAsync ("Xyz"); // After that, I planned to do GetFileAsync. But this also leads to a folder error not found.

And finally, I tried S = wait for S. GetFolderAsync ("D");

Any help regarding how I access the file or even an alternative way to achieve my goal would be fantastic!

PS: StorageFile.GetFileFromPathAsync ("D: \ Xyz \ abc.mp3") says that I do not have access to access.

+8
windows windows-phone
source share
3 answers

(Answer the adjustment to be more clear)

If you specified in the package.appxmanifest file:

  • Music Library features in features
  • File Type Association in ads (I'm not sure if this is necessary if you only get access to the Music Library)

then you can receive files from the Music Library folder as follows:

 StorageFolder folder = KnownFolders.MusicLibrary; // below code will work unless 'fileName.mp3' is in // your Music folder either on phone or SD card StorageFile file = await folder.GetFileAsync("fileName.mp3"); // you can access them also with path, but in this case // also Folders must be in Music folder string path = @"Test\ccc.mp3"; StorageFile file = await folder.GetFileAsync(path); 

To summarize, you can use KnownFolders.MusicLibrary only files that are located either on the phone or on the SD card, but in the Music folder - their paths will be as follows:

C: \ Data \ Users \ Public \ Music \ test.mp3 - for phone

D: \ Music \ Test \ test2.mp3 - for an SD card

Remember that access to StorageFolder.GetFileAsync () works relative in StorageFolder (it will also work with the full path, see note below):

The name (or path to the current folder) of the file to extract.

In this case, to get the files above, you will use file names such as: test.mp3 and Test\test2.mp3 .

You will not be able to receive other files, such as D:\xxx.mp3 - this is not in the music library - it is on the SD card, if you want to access it, you will need to do this KnownFolders.RemovableDevices - when you added the required features then you can access this via fullpath. But also remember the remark:

Do not use this property to access the file, because some files may not have paths to the file system. For example, if a file is supported by a URI or was selected using the file collector, the file cannot guarantee the path to the file system.

In our discussion, it turns out that you are using CommonFileQuery.OrderByName to get the files. Since I checked there (IMO), there may be a slight error - the VS description says that:

The query results for deep queries include all files in all subfolders of the requested folder and sort them based on the specified metadata.

Running code code:

 // a method to retrieve files from Musiic Library recursively private async Task RetriveFilesInFolders(List<StorageFile> list, StorageFolder parent) { foreach (var item in await parent.GetFilesAsync()) list.Add(item); foreach (var item in await parent.GetFoldersAsync()) await RetriveFilesInFolders(list, item); } // then I've used it like this: StorageFolder folder = KnownFolders.MusicLibrary; List<StorageFile> listOfFiles = new List<StorageFile>(); await RetriveFilesInFolders(listOfFiles, folder); // as a result of above code I have a List of 5 files that are in Music Library List<IStorageItem> filesFolders = (await folder.GetItemsAsync()).ToList(); List<StorageFile> items = (await folder.GetFilesAsync(CommonFileQuery.OrderByName)).ToList(); // as a result here I've 14 files that are in Music Library and SD card, and Phone 

gave resuls:

  • in the first case, 5 files that are actually located in the "Music" folder and subfolders,
  • in the second case - 14 files located in: Music library, SD-card and phone

You can easily view Path in debug mode, and you will see that some of the files in the second method have D:\file.mp3 , etc. - they are not in the music library, so you cannot get them from KnownFolders.MusicLibrary . If you want to get them, you will have to use other KnownFolders - in this case the SD card:

 StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices; StorageFolder sdCard = (await externalDevices.GetFoldersAsync()).FirstOrDefault(); StorageFile file = await sdCard.GetFileAsync("xyz.mp3"); // for D:\xyz.mp3 StorageFile file = await sdCard.GetFileAsync("XYZ\xyz.mp3"); // for D:\XYZ\xyz.mp3 
+10
source share

First, make sure you have the appropriate features installed. The error you get is what you will see if you did not specify the correct CAP_ID. Here is a screenshot:

enter image description here

As for file paths, depending on where the file is stored, you may need to use a double slash. I had a similar problem, I decided to use the following approach:

"isostore: common \\ FOLDERNAME \\ NameOfFile.mp3"

You can see the whole process in my SO post here .

+2
source share

You want to use an access cache to store file access instead of paths. This will allow you to restore StorageFile objects that you can work with right away, and will help in some scenarios involving moving files.

To save a file in the access cache

  StorageFile file; //Get a file string mruToken = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "20120716"); 

Then you save the token somewhere. To return a file from the cache:

  StorageFile file = await Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.getFileAsync(mruToken); 

Saving only the path opens up problems with changing drive letters. This ensures that if the file is still available, your application can receive it.

+1
source share

All Articles