Hendra Anggrian Hendra Anggrian - 3 months ago 18
C# Question

Windows.Storage.KnownFolders.MusicLibrary path returns empty

I'm trying to get default music library folder path in Windows Universal App written in C#. However,

KnownFolders.MusicLibrary.Path
returns empty string while
(await KnownFolders.MusicLibrary.GetFoldersAsync())
seem to return empty list. I already specified this capability in
Package.appxmanifest
:

<Package ...>

...

<Capabilities>
<uap:Capability Name="musicLibrary" />
</Capabilities>
</Package>


How am I doing this wrong? I expect that line of code will return something like
C:\Users\username\Music
.

Answer

In the Remarks of KnownFolders.MusicLibrary property, we can find that

Libraries let users view related content in a single location. A library is a virtual folder. The folders in a library remain in their original locations. Learn more about libraries in Windows Libraries.

StorageFolder.Path property can get the full path of the current folder in the file system, but only if the path is available. As Music Library is a virtual folder, the file system path is not available. So it always returns empty string.

The Music Library typically has the following path.

%USERPROFILE%\Music

This is the path of the known folder included in Music Library.

A library is a virtual collection of folders, which includes a known folder by default plus any other folders the user has added to the library by using your app or one of the built-in apps. For example, the Pictures library includes the Pictures known folder by default. The user can add folders to, or remove them from, the Pictures library by using your app or the built-in Photos app.

To get this path, we can use StorageLibrary class. This class lets us add and remove folders from a library like Music or Videos and lets us get a list of folders included in the library.

The known folder is the folder in a library where new files are saved by default. We can use StorageLibrary.SaveFolder property to get this folder and than get its path like following:

var musicLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Music);
Debug.WriteLine(musicLibrary.SaveFolder.Path);

For more info, please see Files and folders in the Music, Pictures, and Videos libraries.

Comments