/// <summary> /// Checks whether a folder or file exists at the given location. /// </summary> /// <param name="name">The name of the file or folder to check for.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns> /// A task whose result is the result of the existence check. /// </returns> public async Task <ExistenceCheckResult> CheckExistsAsync(string name, CancellationToken cancellationToken) { Requires.NotNullOrEmpty(name, "name"); // WinRT does not expose an Exists method, so we have to // try accessing the entity to see if it succeeds. // We could code this up with a catch block, but that means // that a file existence check requires first chance exceptions // are thrown and caught, which *can* slow the app down, // and also bugs the developer who is debugging the app. // So we just avoid all exceptions being *thrown* // by checking for exception objects carefully. var result = await _wrappedFolder.GetItemAsync(name).AsTaskNoThrow(cancellationToken); if (result.IsFaulted) { if (result.Exception.InnerException is FileNotFoundException) { return(ExistenceCheckResult.NotFound); } else { // rethrow unexpected exceptions. result.GetAwaiter().GetResult(); throw result.Exception; // shouldn't reach here anyway. } } else if (result.IsCanceled) { throw new OperationCanceledException(); } else { Windows.Storage.IStorageItem storageItem = result.Result; if (storageItem.IsOfType(StorageItemTypes.File)) { return(ExistenceCheckResult.FileExists); } else if (storageItem.IsOfType(StorageItemTypes.Folder)) { return(ExistenceCheckResult.FolderExists); } else { return(ExistenceCheckResult.NotFound); } } }
public async void PopulateRecentsList() { var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList; BitmapImage ItemImage = new BitmapImage(); string ItemPath = null; string ItemName; StorageItemTypes ItemType; Visibility ItemFolderImgVis; Visibility ItemEmptyImgVis; Visibility ItemFileIconVis; if (mostRecentlyUsed.Entries.Count == 0) { Empty.Visibility = Visibility.Visible; } else { Empty.Visibility = Visibility.Collapsed; } foreach (Windows.Storage.AccessCache.AccessListEntry entry in mostRecentlyUsed.Entries) { string mruToken = entry.Token; try { Windows.Storage.IStorageItem item = await mostRecentlyUsed.GetItemAsync(mruToken); if (item.IsOfType(StorageItemTypes.File)) { ItemName = item.Name; ItemPath = item.Path; ItemType = StorageItemTypes.File; ItemImage = new BitmapImage(); StorageFile file = await StorageFile.GetFileFromPathAsync(ItemPath); var thumbnail = await file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 30, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail); if (thumbnail == null) { ItemEmptyImgVis = Visibility.Visible; } else { await ItemImage.SetSourceAsync(thumbnail.CloneStream()); ItemEmptyImgVis = Visibility.Collapsed; } ItemFolderImgVis = Visibility.Collapsed; ItemFileIconVis = Visibility.Visible; recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, type = ItemType, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }); } } catch (System.IO.FileNotFoundException) { mostRecentlyUsed.Remove(mruToken); } catch (UnauthorizedAccessException) { // Skip item until consent is provided } } }