uwp javascript列出音乐库文件夹中的文件

时间:2017-10-09 23:28:08

标签: javascript uwp

以下列出了应用程序中“资产”文件夹中的文件并且功能正常。

// Get the path to the app's Assets folder.
var root = Windows.ApplicationModel.Package.current.installedLocation.path;
var path = root + "\\Assets";
//var path = Windows.Storage.KnownFolders.musicLibrary; 
var StorageFolder = Windows.Storage.StorageFolder;
var folderPromise = StorageFolder.getFolderFromPathAsync(path);
folderPromise.done(function getFolderSuccess(folder) {
    var filesInFolderPromise = folder.getFilesAsync();
    filesInFolderPromise.done(function getFilesSuccess(filesInFolder) {
        s = "";
        filesInFolder.forEach(function forEachFile(item) {
            s = s + item.name + "<br />";
        });
        document.getElementById('filelist').innerHTML = s;
    });
});

我想修改它以列出musicLibrary中的文件。当我将路径名称更改为

var path = Windows.Storage.KnownFolders.musicLibrary; 

我得到了

JavaScript runtime error: The parameter is incorrect

请帮助我使用适当的语法。 musicLibrary功能在清单中设置。

3 个答案:

答案 0 :(得分:1)

Windows.Storage.KnownFolders.musicLibrary

不是路径字符串。这是StorageFolder类。因此,getFolderFromPathAsync的调用失败。请参阅Microsoft文档。

KnownFolders Class (You can select 'JavaScript' from the language combobox on the right pane)

答案 1 :(得分:1)

该对象是从Music文件夹中获取文件异步。这可以直接完成如下:

var f = Windows.Storage.KnownFolders.musicLibrary;
f.getFilesAsync().done(function getFilesSuccess(filelist) {
    s = "";
    filelist.forEach(function forEachFile(item) {
        s = s + item.name + "<br />";
    });
    document.getElementById('filelist').innerHTML = s;
});

感谢pnp0a03,并快速阅读文档。

答案 2 :(得分:0)

对于那些在语法上苦苦挣扎的人来说,这个答案是等价的:

var f = Windows.Storage.KnownFolders.musicLibrary;
f.getFilesAsync().then(success42);
function success42(filelist) {
    s = "";
    filelist.forEach(function forEachFile(item) {
        s = s + item.name + "<br />";
    });
    document.getElementById('filelist').innerHTML = s;
}