Windows.Storage.KnownFolders.MusicLibrary 路径返回空 [英] Windows.Storage.KnownFolders.MusicLibrary path returns empty

查看:26
本文介绍了Windows.Storage.KnownFolders.MusicLibrary 路径返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用 C# 编写的 Windows 通用应用程序中获取默认音乐库文件夹路径.但是,KnownFolders.MusicLibrary.Path 返回空字符串而 (await KnownFolders.MusicLibrary.GetFoldersAsync()) 似乎返回空列表.我已经在 Package.appxmanifest 中指定了这个功能:

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>

我怎么做错了?我希望这行代码会返回类似 C:\Users\username\Music 的内容.

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

推荐答案

KnownFolders.MusicLibrary 属性,我们可以发现

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

图书馆让用户可以在一个位置查看相关内容.库是一个虚拟文件夹.库中的文件夹保留在其原始位置.在 Windows 库.

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 属性 可以获取文件系统中当前文件夹的完整路径,但前提是路径可用.由于音乐库是一个虚拟文件夹,文件系统路径不可用.所以它总是返回空字符串.

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.

要获取此路径,我们可以使用 StorageLibrary 类.这个类允许我们从音乐或视频等库中添加和删除文件夹,并让我们获取库中包含的文件夹列表.

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.

已知文件夹是库中默认保存新文件的文件夹.我们可以使用 StorageLibrary.SaveFolder 属性 来获取这个文件夹,而不是像这样获取它的路径以下:

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.

这篇关于Windows.Storage.KnownFolders.MusicLibrary 路径返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆