在窗口电话8.1中递归查找子文件夹文件计数

时间:2014-09-13 01:38:04

标签: c# windows-phone windows-phone-8.1

我正在尝试获取KnownFolder.MusicLibrary或任何媒体类中包含的文件数。我无法获取艺术家文件夹中子文件夹的计数。我能做到吗?

1 个答案:

答案 0 :(得分:1)

试试这段代码。但这个问题已经有3个月了。所以,我希望你已经找到了一种方法来做到这一点。如果是,请发布您的答案。对其他人来说可能会派上用场。

访问此link。它有一个非常好的文档。在我使用GetFoldersAsync()从视频库访问文件夹后,我传递了一个函数。并且该函数遍历每个视频库项目&检查它是文件夹还是文件。如果是文件夹,则使用GetItemsAsync()&找到驻留在函数内的内容。它的数量已确定&回。如果它已经是文件,则会返回它的计数。

希望这会有所帮助。如果它有用,请将其标记为答案。

    IReadOnlyList<IStorageItem> VideoLibrary = await KnownFolders.VideosLibrary.GetFoldersAsync();
    int count= GetCount(VideoLibrary);
    private async Task<string> GetCount(IReadOnlyList<IStorageItem> VideoLibraryItems)
    {
            foreach (IStorageItem vItem in VideoLibraryItems)
            {
                IStorageItem item = vItem;
                if (item.IsOfType(Windows.Storage.StorageItemTypes.Folder))
                {
                    StorageFolder sfolder = (StorageFolder)item;
                    IReadOnlyList<IStorageItem> fileList = await sfolder.GetItemsAsync();
                    return fileList.Count
        }
                else if(item.IsOfType(Windows.Storage.StorageItemTypes.File))
        {
                    StorageFile sf = (StorageFile)item;
        return sf.Count;
                }
            }
    }