问题描述
嘿,我有以下内容:
var fileList = await KnownFolders.MusicLibrary.GetItemsAsync();
它不返回任何文件,我在 Music 文件夹中有文件.我也有:
It doesn't return any files and i have files in the Music folder. I also have :
<Capabilities>
<Capability Name="internetClient" />
<Capability Name="removableStorage" />
<Capability Name="documentsLibrary" />
<Capability Name="MusicLibrary" />
<Capability Name="HomeGroup" />
<Capability Name="RemovableDevices" />
我不知道为什么它不返回任何文件/异常?有什么建议 ?我也尝试过 FolderPicker 来获取文件夹中的所有文件,但结果相同.
I dont know why it doesnt return any files/ exception ? Any suggestions ? I also tried FolderPicker to get all files in a folder , but same result.
推荐答案
根据您发布的代码,您似乎使用了错误的功能.
Form the code you've posted, it seems you are using wrong capabilities.
当您在应用的包清单中声明 musicLibrary 功能时,它必须包含 uap 命名空间,如下所示.
<Capabilities><uap:Capability Name="musicLibrary"/></Capabilities>
有关详细信息,请参阅应用功能声明.
For more info, please see App capability declarations.
所以你可以像下面这样修改你的Package.appxmanifest
:
So you can chane your Package.appxmanifest
like the following:
<Capabilities>
<Capability Name="internetClient" />
<uap:Capability Name="removableStorage" />
<uap:Capability Name="musicLibrary" />
<uap:Capability Name="documentsLibrary" />
</Capabilities>
然后您应该能够获取音乐库中的文件和子文件夹.
And then you should be able to get the files and subfolders in the music library.
var fileList = await KnownFolders.MusicLibrary.GetItemsAsync();
if (fileList.Count > 0)
{
foreach (var item in fileList)
{
Debug.WriteLine(item.Name);
}
}
音乐库通常具有以下路径.
The Music Library typically has the following path.
%USERPROFILE%\Music
您可以检查此路径下是否有文件.您也可以使用以下代码检查路径.
You can check if you have files under this path. And also you can check the path with the following code.
var musicLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Music);
Debug.WriteLine(musicLibrary.SaveFolder.Path);
这将输出已知文件夹的路径,该文件夹是库中默认保存新文件的文件夹.有关详细信息,请参阅此答案.
This will output the path of the known folder, which is the folder in a library where new files are saved by default. For more info, please see this answer.
这篇关于访问音乐文件 UWP -KnownFolders.MusicLibrary.GetItemsAsync() 不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!