Esempio n. 1
0
        private static async Task <IReadOnlyList <IStorageItem> > EnumerateFileByFile(BaseStorageFolder rootFolder, uint startFrom, uint itemsToIterate)
        {
            var tempList = new List <IStorageItem>();

            for (var i = startFrom; i < startFrom + itemsToIterate; i++)
            {
                IStorageItem item;
                try
                {
                    var results = await rootFolder.GetItemsAsync(i, 1);

                    item = results?.FirstOrDefault();
                    if (item == null)
                    {
                        break;
                    }
                }
                catch (NotImplementedException)
                {
                    break;
                }
                catch (Exception ex) when(
                    ex is UnauthorizedAccessException ||
                    ex is FileNotFoundException ||
                    (uint)ex.HResult == 0x80070490)    // ERROR_NOT_FOUND
                {
                    continue;
                }
                tempList.Add(item);
            }
            return(tempList);
        }
Esempio n. 2
0
        public static async Task <List <ListedItem> > ListEntries(
            BaseStorageFolder rootFolder,
            StorageFolderWithPath currentStorageFolder,
            string returnformat,
            Type sourcePageType,
            CancellationToken cancellationToken,
            int countLimit,
            Func <List <ListedItem>, Task> intermediateAction,
            Dictionary <string, BitmapImage> defaultIconPairs = null
            )
        {
            var  sampler    = new IntervalSampler(500);
            var  tempList   = new List <ListedItem>();
            uint count      = 0;
            var  firstRound = true;

            while (true)
            {
                IReadOnlyList <IStorageItem> items;
                uint maxItemsToRetrieve = 300;

                if (intermediateAction == null)
                {
                    // without intermediate action increase batches significantly
                    maxItemsToRetrieve = 1000;
                }
                else if (firstRound)
                {
                    maxItemsToRetrieve = 32;
                    firstRound         = false;
                }
                try
                {
                    items = await rootFolder.GetItemsAsync(count, maxItemsToRetrieve);

                    if (items == null || items.Count == 0)
                    {
                        break;
                    }
                }
                catch (NotImplementedException)
                {
                    break;
                }
                catch (Exception ex) when(
                    ex is UnauthorizedAccessException ||
                    ex is FileNotFoundException ||
                    (uint)ex.HResult == 0x80070490)    // ERROR_NOT_FOUND
                {
                    // If some unexpected exception is thrown - enumerate this folder file by file - just to be sure
                    items = await EnumerateFileByFile(rootFolder, count, maxItemsToRetrieve);
                }
                foreach (var item in items)
                {
                    if (item.IsOfType(StorageItemTypes.Folder))
                    {
                        var folder = await AddFolderAsync(item.AsBaseStorageFolder(), currentStorageFolder, returnformat, cancellationToken);

                        if (folder != null)
                        {
                            if (defaultIconPairs?.ContainsKey(string.Empty) ?? false)
                            {
                                folder.SetDefaultIcon(defaultIconPairs[string.Empty]);
                            }
                            tempList.Add(folder);
                        }
                    }
                    else
                    {
                        var fileEntry = await AddFileAsync(item.AsBaseStorageFile(), currentStorageFolder, returnformat, cancellationToken);

                        if (fileEntry != null)
                        {
                            if (defaultIconPairs != null)
                            {
                                if (!string.IsNullOrEmpty(fileEntry.FileExtension))
                                {
                                    var lowercaseExtension = fileEntry.FileExtension.ToLowerInvariant();
                                    if (defaultIconPairs.ContainsKey(lowercaseExtension))
                                    {
                                        fileEntry.SetDefaultIcon(defaultIconPairs[lowercaseExtension]);
                                    }
                                }
                            }
                            tempList.Add(fileEntry);
                        }
                    }
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }
                }
                count += maxItemsToRetrieve;

                if (countLimit > -1 && count >= countLimit)
                {
                    break;
                }

                if (intermediateAction != null && (items.Count == maxItemsToRetrieve || sampler.CheckNow()))
                {
                    await intermediateAction(tempList);

                    // clear the temporary list every time we do an intermediate action
                    tempList.Clear();
                }
            }
            return(tempList);
        }