private static async Task <ListedItem> AddFolderAsync(StorageFolder folder, StorageFolderWithPath currentStorageFolder, string dateReturnFormat, CancellationToken cancellationToken) { var basicProperties = await folder.GetBasicPropertiesAsync(); var extraProps = await basicProperties.RetrievePropertiesAsync(new[] { "System.DateCreated" }); DateTimeOffset.TryParse(extraProps["System.DateCreated"] as string, out var dateCreated); if (!cancellationToken.IsCancellationRequested) { return(new ListedItem(folder.FolderRelativeId, dateReturnFormat) { PrimaryItemAttribute = StorageItemTypes.Folder, ItemName = folder.DisplayName, ItemDateModifiedReal = basicProperties.DateModified, ItemDateCreatedReal = dateCreated, ItemType = folder.DisplayType, IsHiddenItem = false, Opacity = 1, LoadFolderGlyph = true, FileImage = null, LoadFileIcon = false, ItemPath = string.IsNullOrEmpty(folder.Path) ? Path.Combine(currentStorageFolder.Path, folder.Name) : folder.Path, LoadUnknownTypeGlyph = false, FileSize = null, FileSizeBytes = 0 }); } return(null); }
private static async Task <ListedItem> AddFileAsync( BaseStorageFile file, StorageFolderWithPath currentStorageFolder, string dateReturnFormat, CancellationToken cancellationToken ) { var basicProperties = await file.GetBasicPropertiesAsync(); // Display name does not include extension var itemName = file.Name; var itemModifiedDate = basicProperties.DateModified; var itemCreatedDate = file.DateCreated; var itemPath = string.IsNullOrEmpty(file.Path) ? PathNormalization.Combine(currentStorageFolder.Path, file.Name) : file.Path; var itemSize = basicProperties.Size.ToSizeString(); var itemSizeBytes = basicProperties.Size; var itemType = file.DisplayType; var itemFileExtension = file.FileType; var itemThumbnailImgVis = false; if (cancellationToken.IsCancellationRequested) { return(null); } if (file.Name.EndsWith(".lnk", StringComparison.Ordinal) || file.Name.EndsWith(".url", StringComparison.Ordinal)) { // This shouldn't happen, StorageFile api does not support shortcuts Debug.WriteLine("Something strange: StorageFile api returned a shortcut"); } // TODO: is this needed to be handled here? else if (App.LibraryManager.TryGetLibrary(file.Path, out LibraryLocationItem library)) { return(new LibraryItem(library) { ItemDateModifiedReal = itemModifiedDate, ItemDateCreatedReal = itemCreatedDate, }); } else { return(new ListedItem(file.FolderRelativeId, dateReturnFormat) { PrimaryItemAttribute = StorageItemTypes.File, FileExtension = itemFileExtension, IsHiddenItem = false, Opacity = 1, FileImage = null, LoadFileIcon = itemThumbnailImgVis, ItemNameRaw = itemName, ItemDateModifiedReal = itemModifiedDate, ItemDateCreatedReal = itemCreatedDate, ItemType = itemType, ItemPath = itemPath, FileSize = itemSize, FileSizeBytes = (long)itemSizeBytes, }); } return(null); }
public static async Task <IStorageItemWithPath> ToStorageItemWithPath(this string path, StorageFolderWithPath parentFolder = null) { StorageFolderWithPath rootFolder = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(path)); FilesystemResult <StorageFileWithPath> fsFileWithPathResult = await FilesystemTasks.Wrap(() => { return(StorageFileExtensions.DangerousGetFileWithPathFromPathAsync(path, rootFolder, parentFolder)); }); if (fsFileWithPathResult) { return(fsFileWithPathResult.Result); } FilesystemResult <StorageFolderWithPath> fsFolderWithPathResult = await FilesystemTasks.Wrap(() => { return(StorageFileExtensions.DangerousGetFolderWithPathFromPathAsync(path, rootFolder)); }); if (fsFolderWithPathResult) { return(fsFolderWithPathResult.Result); } return(null); }
// TODO: If the TODO of IStorageItemWithPath is implemented, change return type to IStorageItem public static async Task <IStorageItem> ToStorageItem(this string path, StorageFolderWithPath parentFolder = null) { FilesystemResult <StorageFolderWithPath> fsRootFolderResult = await FilesystemTasks.Wrap(async() => { return((StorageFolderWithPath)await Path.GetPathRoot(path).ToStorageItemWithPath()); }); FilesystemResult <StorageFile> fsFileResult = await FilesystemTasks.Wrap(() => { return(StorageFileExtensions.DangerousGetFileFromPathAsync(path, fsRootFolderResult.Result, parentFolder)); }); if (fsFileResult) { if (!string.IsNullOrWhiteSpace(fsFileResult.Result.Path)) { return(fsFileResult.Result); } else { FilesystemResult <StorageFileWithPath> fsFileWithPathResult = await FilesystemTasks.Wrap(() => { return(StorageFileExtensions.DangerousGetFileWithPathFromPathAsync(path, fsRootFolderResult)); }); if (fsFileWithPathResult) { return(null); /* fsFileWithPathResult.Result */ // Could be done if IStorageItemWithPath implemented IStorageItem } } } FilesystemResult <StorageFolder> fsFolderResult = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path)); if (fsFolderResult) { if (!string.IsNullOrWhiteSpace(fsFolderResult.Result.Path)) { return(fsFolderResult.Result); } else { FilesystemResult <StorageFolderWithPath> fsFolderWithPathResult = await FilesystemTasks.Wrap(() => { return(StorageFileExtensions.DangerousGetFolderWithPathFromPathAsync(path, fsRootFolderResult)); }); if (fsFolderWithPathResult) { return(null); /* fsFolderWithPathResult.Result; */ // Could be done if IStorageItemWithPath implemented IStorageItem } } } return(null); }
public static async void CreateFile(AddItemType itemType) { string currentPath = null; if (App.CurrentInstance.ContentPage != null) { currentPath = App.CurrentInstance.FilesystemViewModel.WorkingDirectory; } StorageFolderWithPath folderWithPath = await ItemViewModel.GetFolderWithPathFromPathAsync(currentPath); StorageFolder folderToCreateItem = folderWithPath.Folder; // Show rename dialog RenameDialog renameDialog = new RenameDialog(); var renameResult = await renameDialog.ShowAsync(); if (renameResult != ContentDialogResult.Primary) { return; } // Create file based on dialog result string userInput = renameDialog.storedRenameInput; try { switch (itemType) { case AddItemType.Folder: userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : "NewFolder".GetLocalized(); await folderToCreateItem.CreateFolderAsync(userInput, CreationCollisionOption.GenerateUniqueName); break; case AddItemType.TextDocument: userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : "NewTextDocument".GetLocalized(); await folderToCreateItem.CreateFileAsync(userInput + ".txt", CreationCollisionOption.GenerateUniqueName); break; case AddItemType.BitmapImage: userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : "NewBitmapImage".GetLocalized(); await folderToCreateItem.CreateFileAsync(userInput + ".bmp", CreationCollisionOption.GenerateUniqueName); break; } } catch (UnauthorizedAccessException) { await DialogDisplayHelper.ShowDialog("AccessDeniedCreateDialog/Title".GetLocalized(), "AccessDeniedCreateDialog/Text".GetLocalized()); } }
public static async void CreateFile(AddItemType itemType) { string currentPath = null; if (App.CurrentInstance.ContentPage != null) { currentPath = App.CurrentInstance.FilesystemViewModel.WorkingDirectory; } StorageFolderWithPath folderWithPath = await ItemViewModel.GetFolderWithPathFromPathAsync(currentPath); StorageFolder folderToCreateItem = folderWithPath.Folder; // Show rename dialog RenameDialog renameDialog = new RenameDialog(); var renameResult = await renameDialog.ShowAsync(); if (renameResult != ContentDialogResult.Primary) { return; } // Create file based on dialog result string userInput = renameDialog.storedRenameInput; switch (itemType) { case AddItemType.Folder: userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : ResourceController.GetTranslation("NewFolder"); await folderToCreateItem.CreateFolderAsync(userInput, CreationCollisionOption.GenerateUniqueName); break; case AddItemType.TextDocument: userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : ResourceController.GetTranslation("NewTextDocument"); await folderToCreateItem.CreateFileAsync(userInput + ".txt", CreationCollisionOption.GenerateUniqueName); break; case AddItemType.BitmapImage: userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : ResourceController.GetTranslation("NewBitmapImage"); await folderToCreateItem.CreateFileAsync(userInput + ".bmp", CreationCollisionOption.GenerateUniqueName); break; } }
public static async Task <List <ListedItem> > ListEntries( StorageFolder rootFolder, StorageFolderWithPath currentStorageFolder, string returnformat, Type sourcePageType, CancellationToken cancellationToken, int countLimit, Func <List <ListedItem>, Task> intermediateAction ) { 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 as StorageFolder, currentStorageFolder, returnformat, cancellationToken); if (folder != null) { tempList.Add(folder); } } else { var file = item as StorageFile; var fileEntry = await AddFileAsync(file, currentStorageFolder, returnformat, true, sourcePageType, cancellationToken); if (fileEntry != null) { 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); }
private static async Task <ListedItem> AddFileAsync( StorageFile file, StorageFolderWithPath currentStorageFolder, string dateReturnFormat, bool suppressThumbnailLoading, Type sourcePageType, CancellationToken cancellationToken ) { var basicProperties = await file.GetBasicPropertiesAsync(); var extraProperties = await basicProperties.RetrievePropertiesAsync(new[] { "System.DateCreated" }); // Display name does not include extension var itemName = string.IsNullOrEmpty(file.DisplayName) || App.AppSettings.ShowFileExtensions ? file.Name : file.DisplayName; var itemModifiedDate = basicProperties.DateModified; DateTimeOffset.TryParse(extraProperties["System.DateCreated"] as string, out var itemCreatedDate); var itemPath = string.IsNullOrEmpty(file.Path) ? Path.Combine(currentStorageFolder.Path, file.Name) : file.Path; var itemSize = ByteSize.FromBytes(basicProperties.Size).ToBinaryString().ConvertSizeAbbreviation(); var itemSizeBytes = basicProperties.Size; var itemType = file.DisplayType; var itemFolderImgVis = false; var itemFileExtension = file.FileType; BitmapImage icon = new BitmapImage(); byte[] iconData = null; bool itemThumbnailImgVis; bool itemEmptyImgVis; if (sourcePageType != typeof(GridViewBrowser)) { try { var itemThumbnailImg = suppressThumbnailLoading ? null : await file.GetThumbnailAsync(ThumbnailMode.ListView, 40, ThumbnailOptions.UseCurrentScale); if (itemThumbnailImg != null) { itemEmptyImgVis = false; itemThumbnailImgVis = true; icon.DecodePixelWidth = 40; icon.DecodePixelHeight = 40; await icon.SetSourceAsync(itemThumbnailImg); iconData = await itemThumbnailImg.ToByteArrayAsync(); } else { itemEmptyImgVis = true; itemThumbnailImgVis = false; } } catch { itemEmptyImgVis = true; itemThumbnailImgVis = false; // Catch here to avoid crash } } else { try { var itemThumbnailImg = suppressThumbnailLoading ? null : await file.GetThumbnailAsync(ThumbnailMode.ListView, 80, ThumbnailOptions.UseCurrentScale); if (itemThumbnailImg != null) { itemEmptyImgVis = false; itemThumbnailImgVis = true; icon.DecodePixelWidth = 80; icon.DecodePixelHeight = 80; await icon.SetSourceAsync(itemThumbnailImg); iconData = await itemThumbnailImg.ToByteArrayAsync(); } else { itemEmptyImgVis = true; itemThumbnailImgVis = false; } } catch { itemEmptyImgVis = true; itemThumbnailImgVis = false; } } if (cancellationToken.IsCancellationRequested) { return(null); } if (file.Name.EndsWith(".lnk") || file.Name.EndsWith(".url")) { // This shouldn't happen, StorageFile api does not support shortcuts Debug.WriteLine("Something strange: StorageFile api returned a shortcut"); } // TODO: is this needed to be handled here? else if (App.LibraryManager.TryGetLibrary(file.Path, out LibraryLocationItem library)) { return(new LibraryItem(library) { ItemDateModifiedReal = itemModifiedDate, ItemDateCreatedReal = itemCreatedDate, }); } else { return(new ListedItem(file.FolderRelativeId, dateReturnFormat) { PrimaryItemAttribute = StorageItemTypes.File, FileExtension = itemFileExtension, IsHiddenItem = false, Opacity = 1, LoadUnknownTypeGlyph = itemEmptyImgVis, FileImage = icon, CustomIconData = iconData, LoadFileIcon = itemThumbnailImgVis, LoadFolderGlyph = itemFolderImgVis, ItemName = itemName, ItemDateModifiedReal = itemModifiedDate, ItemDateCreatedReal = itemCreatedDate, ItemType = itemType, ItemPath = itemPath, FileSize = itemSize, FileSizeBytes = (long)itemSizeBytes, }); } return(null); }
private static async Task <ListedItem> AddFolderAsync(BaseStorageFolder folder, StorageFolderWithPath currentStorageFolder, string dateReturnFormat, CancellationToken cancellationToken) { var basicProperties = await folder.GetBasicPropertiesAsync(); if (!cancellationToken.IsCancellationRequested) { return(new ListedItem(folder.FolderRelativeId, dateReturnFormat) { PrimaryItemAttribute = StorageItemTypes.Folder, ItemName = folder.DisplayName, ItemDateModifiedReal = basicProperties.DateModified, ItemDateCreatedReal = folder.DateCreated, ItemType = folder.DisplayType, IsHiddenItem = false, Opacity = 1, FileImage = null, LoadFileIcon = false, ItemPath = string.IsNullOrEmpty(folder.Path) ? PathNormalization.Combine(currentStorageFolder.Path, folder.Name) : folder.Path, FileSize = null, FileSizeBytes = 0 }); } return(null); }
public static async Task <ListedItem> AddFileAsync( BaseStorageFile file, StorageFolderWithPath currentStorageFolder, CancellationToken cancellationToken ) { var basicProperties = await file.GetBasicPropertiesAsync(); // Display name does not include extension var itemName = file.Name; var itemModifiedDate = basicProperties.DateModified; var itemCreatedDate = file.DateCreated; var itemPath = string.IsNullOrEmpty(file.Path) ? PathNormalization.Combine(currentStorageFolder.Path, file.Name) : file.Path; var itemSize = basicProperties.Size.ToSizeString(); var itemSizeBytes = basicProperties.Size; var itemType = file.DisplayType; var itemFileExtension = file.FileType; var itemThumbnailImgVis = false; if (cancellationToken.IsCancellationRequested) { return(null); } // TODO: is this needed to be handled here? if (App.LibraryManager.TryGetLibrary(file.Path, out LibraryLocationItem library)) { return(new LibraryItem(library) { ItemDateModifiedReal = itemModifiedDate, ItemDateCreatedReal = itemCreatedDate, }); } else { if (file is ShortcutStorageFile linkFile) { var isUrl = linkFile.Name.EndsWith(".url", StringComparison.OrdinalIgnoreCase); return(new ShortcutItem(file.FolderRelativeId) { PrimaryItemAttribute = StorageItemTypes.File, FileExtension = itemFileExtension, IsHiddenItem = false, Opacity = 1, FileImage = null, LoadFileIcon = itemThumbnailImgVis, LoadWebShortcutGlyph = isUrl, ItemNameRaw = itemName, ItemDateModifiedReal = itemModifiedDate, ItemDateCreatedReal = itemCreatedDate, ItemType = itemType, ItemPath = itemPath, FileSize = itemSize, FileSizeBytes = (long)itemSizeBytes, TargetPath = linkFile.TargetPath, Arguments = linkFile.Arguments, WorkingDirectory = linkFile.WorkingDirectory, RunAsAdmin = linkFile.RunAsAdmin, IsUrl = isUrl, }); } else if (file is BinStorageFile binFile) { return(new RecycleBinItem(file.FolderRelativeId) { PrimaryItemAttribute = StorageItemTypes.File, FileExtension = itemFileExtension, IsHiddenItem = false, Opacity = 1, FileImage = null, LoadFileIcon = itemThumbnailImgVis, ItemNameRaw = itemName, ItemDateModifiedReal = itemModifiedDate, ItemDateCreatedReal = itemCreatedDate, ItemType = itemType, ItemPath = itemPath, FileSize = itemSize, FileSizeBytes = (long)itemSizeBytes, ItemDateDeletedReal = binFile.DateDeleted, ItemOriginalPath = binFile.OriginalPath }); } else { return(new ListedItem(file.FolderRelativeId) { PrimaryItemAttribute = StorageItemTypes.File, FileExtension = itemFileExtension, IsHiddenItem = false, Opacity = 1, FileImage = null, LoadFileIcon = itemThumbnailImgVis, ItemNameRaw = itemName, ItemDateModifiedReal = itemModifiedDate, ItemDateCreatedReal = itemCreatedDate, ItemType = itemType, ItemPath = itemPath, FileSize = itemSize, FileSizeBytes = (long)itemSizeBytes, }); } } return(null); }
public static async Task <List <ListedItem> > ListEntries( BaseStorageFolder rootFolder, StorageFolderWithPath currentStorageFolder, 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; IUserSettingsService userSettingsService = Ioc.Default.GetService <IUserSettingsService>(); 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) { var startWithDot = item.Name.StartsWith("."); if (!startWithDot || userSettingsService.PreferencesSettingsService.ShowDotFiles) { if (item.IsOfType(StorageItemTypes.Folder)) { var folder = await AddFolderAsync(item.AsBaseStorageFolder(), currentStorageFolder, 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, 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); }
public static async Task <ListedItem> AddFolderAsync(BaseStorageFolder folder, StorageFolderWithPath currentStorageFolder, CancellationToken cancellationToken) { var basicProperties = await folder.GetBasicPropertiesAsync(); if (!cancellationToken.IsCancellationRequested) { if (folder is ShortcutStorageFolder linkFolder) { return(new ShortcutItem(folder.FolderRelativeId) { PrimaryItemAttribute = StorageItemTypes.Folder, IsHiddenItem = false, Opacity = 1, FileImage = null, LoadFileIcon = false, ItemNameRaw = folder.DisplayName, ItemDateModifiedReal = basicProperties.DateModified, ItemDateCreatedReal = folder.DateCreated, ItemType = folder.DisplayType, ItemPath = folder.Path, FileSize = null, FileSizeBytes = 0, TargetPath = linkFolder.TargetPath, Arguments = linkFolder.Arguments, WorkingDirectory = linkFolder.WorkingDirectory, RunAsAdmin = linkFolder.RunAsAdmin }); } else if (folder is BinStorageFolder binFolder) { return(new RecycleBinItem(folder.FolderRelativeId) { PrimaryItemAttribute = StorageItemTypes.Folder, ItemNameRaw = folder.DisplayName, ItemDateModifiedReal = basicProperties.DateModified, ItemDateCreatedReal = folder.DateCreated, ItemType = folder.DisplayType, IsHiddenItem = false, Opacity = 1, FileImage = null, LoadFileIcon = false, ItemPath = string.IsNullOrEmpty(folder.Path) ? PathNormalization.Combine(currentStorageFolder.Path, folder.Name) : folder.Path, FileSize = null, FileSizeBytes = 0, ItemDateDeletedReal = binFolder.DateDeleted, ItemOriginalPath = binFolder.OriginalPath, }); } else { return(new ListedItem(folder.FolderRelativeId) { PrimaryItemAttribute = StorageItemTypes.Folder, ItemNameRaw = folder.DisplayName, ItemDateModifiedReal = basicProperties.DateModified, ItemDateCreatedReal = folder.DateCreated, ItemType = folder.DisplayType, IsHiddenItem = false, Opacity = 1, FileImage = null, LoadFileIcon = false, ItemPath = string.IsNullOrEmpty(folder.Path) ? PathNormalization.Combine(currentStorageFolder.Path, folder.Name) : folder.Path, FileSize = null, FileSizeBytes = 0 }); } } return(null); }
public static async Task <IEnumerable <IStorageItem> > ToStorageItemCollection(this IEnumerable <ListedItem> listedItems, StorageFolderWithPath parentFolder = null) { List <IStorageItem> output = new List <IStorageItem>(); foreach (ListedItem item in listedItems) { output.Add(await item.ItemPath.ToStorageItem(parentFolder)); } return(output); }
public static async Task <IEnumerable <IStorageItem> > ToStorageItemCollection(this IEnumerable <string> paths, StorageFolderWithPath parentFolder = null) { List <IStorageItem> output = new List <IStorageItem>(); foreach (string path in paths) { output.Add(await path.ToStorageItem(parentFolder)); } return(output); }