private static async Task DeleteItem(StorageDeleteOption deleteOption, IShellPage AppInstance, IProgress <uint> progress) { var deleteFromRecycleBin = AppInstance.FilesystemViewModel.WorkingDirectory.StartsWith(App.AppSettings.RecycleBinPath); List <ListedItem> selectedItems = new List <ListedItem>(); foreach (ListedItem selectedItem in AppInstance.ContentPage.SelectedItems) { selectedItems.Add(selectedItem); } if (App.AppSettings.ShowConfirmDeleteDialog == true) //check if the setting to show a confirmation dialog is on { var dialog = new ConfirmDeleteDialog(deleteFromRecycleBin, deleteOption); await dialog.ShowAsync(); if (dialog.Result != MyResult.Delete) //delete selected item(s) if the result is yes { return; //return if the result isn't delete } deleteOption = dialog.PermanentlyDelete; } int itemsDeleted = 0; foreach (ListedItem storItem in selectedItems) { uint progressValue = (uint)(itemsDeleted * 100.0 / selectedItems.Count); if (selectedItems.Count > 3) { progress.Report((uint)progressValue); } IStorageItem item; try { if (storItem.PrimaryItemAttribute == StorageItemTypes.File) { item = await ItemViewModel.GetFileFromPathAsync(storItem.ItemPath, AppInstance); } else { item = await ItemViewModel.GetFolderFromPathAsync(storItem.ItemPath, AppInstance); } await item.DeleteAsync(deleteOption); } catch (UnauthorizedAccessException) { if (deleteOption == StorageDeleteOption.Default) { // Try again with fulltrust process if (App.Connection != null) { var result = await App.Connection.SendMessageAsync(new ValueSet() { { "Arguments", "FileOperation" }, { "fileop", "MoveToBin" }, { "filepath", storItem.ItemPath } }); } } else { // Try again with DeleteFileFromApp if (!NativeDirectoryChangesHelper.DeleteFileFromApp(storItem.ItemPath)) { Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); } } } catch (FileLoadException) { // try again if (storItem.PrimaryItemAttribute == StorageItemTypes.File) { item = await ItemViewModel.GetFileFromPathAsync(storItem.ItemPath, AppInstance); } else { item = await ItemViewModel.GetFolderFromPathAsync(storItem.ItemPath, AppInstance); } await item.DeleteAsync(deleteOption); } if (deleteFromRecycleBin) { // Recycle bin also stores a file starting with $I for each item var iFilePath = Path.Combine(Path.GetDirectoryName(storItem.ItemPath), Path.GetFileName(storItem.ItemPath).Replace("$R", "$I")); await(await ItemViewModel.GetFileFromPathAsync(iFilePath)).DeleteAsync(StorageDeleteOption.PermanentDelete); } AppInstance.FilesystemViewModel.RemoveFileOrFolder(storItem); itemsDeleted++; } }
private static async Task PasteItem(DataPackageView packageView, string destinationPath, DataPackageOperation acceptedOperation, IShellPage AppInstance, IProgress <uint> progress) { IReadOnlyList <IStorageItem> itemsToPaste = await packageView.GetStorageItemsAsync(); if (!packageView.Contains(StandardDataFormats.StorageItems)) { // Happens if you copy some text and then you Ctrl+V in FilesUWP // Should this be done in ModernShellPage? return; } if (AppInstance.FilesystemViewModel.WorkingDirectory.StartsWith(App.AppSettings.RecycleBinPath)) { // Do not paste files and folders inside the recycle bin await DialogDisplayHelper.ShowDialog(ResourceController.GetTranslation("ErrorDialogThisActionCannotBeDone"), ResourceController.GetTranslation("ErrorDialogUnsupportedOperation")); return; } List <IStorageItem> pastedSourceItems = new List <IStorageItem>(); HashSet <IStorageItem> pastedItems = new HashSet <IStorageItem>(); var totalItemsSize = CalculateTotalItemsSize(itemsToPaste); bool isItemSizeUnreported = totalItemsSize <= 0; foreach (IStorageItem item in itemsToPaste) { if (item.IsOfType(StorageItemTypes.Folder)) { if (!string.IsNullOrEmpty(item.Path) && destinationPath.IsSubPathOf(item.Path)) { ImpossibleActionResponseTypes responseType = ImpossibleActionResponseTypes.Abort; /// Currently following implementation throws exception until it is resolved keep it disabled /*Binding themeBind = new Binding(); * themeBind.Source = ThemeHelper.RootTheme; * * ContentDialog dialog = new ContentDialog() * { * Title = ResourceController.GetTranslation("ErrorDialogThisActionCannotBeDone"), * Content = ResourceController.GetTranslation("ErrorDialogTheDestinationFolder") + " (" + destinationPath.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries).Last() + ") " + ResourceController.GetTranslation("ErrorDialogIsASubfolder") + " (" + item.Name + ")", * PrimaryButtonText = ResourceController.GetTranslation("ErrorDialogSkip"), * CloseButtonText = ResourceController.GetTranslation("ErrorDialogCancel"), * PrimaryButtonCommand = new RelayCommand(() => { responseType = ImpossibleActionResponseTypes.Skip; }), * CloseButtonCommand = new RelayCommand(() => { responseType = ImpossibleActionResponseTypes.Abort; }), * }; * BindingOperations.SetBinding(dialog, FrameworkElement.RequestedThemeProperty, themeBind); * * await dialog.ShowAsync();*/ if (responseType == ImpossibleActionResponseTypes.Skip) { continue; } else if (responseType == ImpossibleActionResponseTypes.Abort) { return; } } else { if (!isItemSizeUnreported) { var pastedItemSize = await Task.Run(() => CalculateTotalItemsSize(pastedSourceItems)); uint progressValue = (uint)(pastedItemSize * 100 / totalItemsSize); progress.Report(progressValue); } try { ClonedDirectoryOutput pastedOutput = await CloneDirectoryAsync( (StorageFolder)item, await ItemViewModel.GetFolderFromPathAsync(destinationPath), item.Name); pastedSourceItems.Add(item); pastedItems.Add(pastedOutput.FolderOutput); } catch (FileNotFoundException) { // Folder was moved/deleted in the meantime continue; } } } else if (item.IsOfType(StorageItemTypes.File)) { if (!isItemSizeUnreported) { var pastedItemSize = await Task.Run(() => CalculateTotalItemsSize(pastedSourceItems)); uint progressValue = (uint)(pastedItemSize * 100 / totalItemsSize); progress.Report(progressValue); } try { StorageFile clipboardFile = (StorageFile)item; StorageFile pastedFile = await clipboardFile.CopyAsync( await ItemViewModel.GetFolderFromPathAsync(destinationPath), item.Name, NameCollisionOption.GenerateUniqueName); pastedSourceItems.Add(item); pastedItems.Add(pastedFile); } catch (UnauthorizedAccessException) { // Try again with CopyFileFromApp if (NativeDirectoryChangesHelper.CopyFileFromApp(item.Path, Path.Combine(destinationPath, item.Name), true)) { pastedSourceItems.Add(item); } else { Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); } } catch (FileNotFoundException) { // File was moved/deleted in the meantime continue; } } } if (!isItemSizeUnreported) { var finalPastedItemSize = await Task.Run(() => CalculateTotalItemsSize(pastedSourceItems)); uint finalProgressValue = (uint)(finalPastedItemSize * 100 / totalItemsSize); progress.Report(finalProgressValue); } else { progress.Report(100); } if (acceptedOperation == DataPackageOperation.Move) { foreach (IStorageItem item in pastedSourceItems) { try { if (string.IsNullOrEmpty(item.Path)) { // Can't move (only copy) files from MTP devices because: // StorageItems returned in DataPackageView are read-only // The item.Path property will be empty and there's no way of retrieving a new StorageItem with R/W access continue; } if (item.IsOfType(StorageItemTypes.File)) { // If we reached this we are not in an MTP device, using StorageFile.* is ok here StorageFile file = await StorageFile.GetFileFromPathAsync(item.Path); await file.DeleteAsync(StorageDeleteOption.PermanentDelete); } else if (item.IsOfType(StorageItemTypes.Folder)) { // If we reached this we are not in an MTP device, using StorageFolder.* is ok here StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(item.Path); await folder.DeleteAsync(StorageDeleteOption.PermanentDelete); } } catch (UnauthorizedAccessException) { // Try again with DeleteFileFromApp if (!NativeDirectoryChangesHelper.DeleteFileFromApp(item.Path)) { Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); } } catch (FileNotFoundException) { // File or Folder was moved/deleted in the meantime continue; } ListedItem listedItem = AppInstance.FilesystemViewModel.FilesAndFolders.FirstOrDefault(listedItem => listedItem.ItemPath.Equals(item.Path, StringComparison.OrdinalIgnoreCase)); } } if (destinationPath == AppInstance.FilesystemViewModel.WorkingDirectory) { List <string> pastedItemPaths = pastedItems.Select(item => item.Path).ToList(); List <ListedItem> copiedItems = AppInstance.FilesystemViewModel.FilesAndFolders.Where(listedItem => pastedItemPaths.Contains(listedItem.ItemPath)).ToList(); if (copiedItems.Any()) { AppInstance.ContentPage.SetSelectedItemsOnUi(copiedItems); AppInstance.ContentPage.FocusSelectedItems(); } } packageView.ReportOperationCompleted(acceptedOperation); }
private async Task <FilesystemResult> DeleteItemAsync(StorageDeleteOption deleteOption, IShellPage AppInstance, IProgress <uint> progress) { var deleted = (FilesystemResult)false; var deleteFromRecycleBin = AppInstance.FilesystemViewModel.WorkingDirectory.StartsWith(App.AppSettings.RecycleBinPath); List <ListedItem> selectedItems = new List <ListedItem>(); foreach (ListedItem selectedItem in AppInstance.ContentPage.SelectedItems) { selectedItems.Add(selectedItem); } if (App.AppSettings.ShowConfirmDeleteDialog == true) //check if the setting to show a confirmation dialog is on { var dialog = new ConfirmDeleteDialog(deleteFromRecycleBin, deleteOption, AppInstance.ContentPage.SelectedItemsPropertiesViewModel); await dialog.ShowAsync(); if (dialog.Result != MyResult.Delete) //delete selected item(s) if the result is yes { return((FilesystemResult)true); //return if the result isn't delete } deleteOption = dialog.PermanentlyDelete; } int itemsDeleted = 0; foreach (ListedItem storItem in selectedItems) { uint progressValue = (uint)(itemsDeleted * 100.0 / selectedItems.Count); if (selectedItems.Count > 3) { progress.Report((uint)progressValue); } if (storItem.PrimaryItemAttribute == StorageItemTypes.File) { deleted = await AppInstance.FilesystemViewModel.GetFileFromPathAsync(storItem.ItemPath) .OnSuccess(t => t.DeleteAsync(deleteOption).AsTask()); } else { deleted = await AppInstance.FilesystemViewModel.GetFolderFromPathAsync(storItem.ItemPath) .OnSuccess(t => t.DeleteAsync(deleteOption).AsTask()); } if (deleted.ErrorCode == FilesystemErrorCode.ERROR_UNAUTHORIZED) { if (deleteOption == StorageDeleteOption.Default) { // Try again with fulltrust process if (AppInstance.FilesystemViewModel.Connection != null) { var response = await AppInstance.FilesystemViewModel.Connection.SendMessageAsync(new ValueSet() { { "Arguments", "FileOperation" }, { "fileop", "MoveToBin" }, { "filepath", storItem.ItemPath } }); deleted = (FilesystemResult)(response.Status == Windows.ApplicationModel.AppService.AppServiceResponseStatus.Success); } } else { // Try again with DeleteFileFromApp if (!NativeDirectoryChangesHelper.DeleteFileFromApp(storItem.ItemPath)) { Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); } else { deleted = (FilesystemResult)true; } } } else if (deleted.ErrorCode == FilesystemErrorCode.ERROR_INUSE) { // TODO: retry or show dialog await DialogDisplayHelper.ShowDialogAsync("FileInUseDeleteDialog/Title".GetLocalized(), "FileInUseDeleteDialog/Text".GetLocalized()); } if (deleteFromRecycleBin) { // Recycle bin also stores a file starting with $I for each item var iFilePath = Path.Combine(Path.GetDirectoryName(storItem.ItemPath), Path.GetFileName(storItem.ItemPath).Replace("$R", "$I")); await AppInstance.FilesystemViewModel.GetFileFromPathAsync(iFilePath) .OnSuccess(t => t.DeleteAsync(StorageDeleteOption.PermanentDelete).AsTask()); } if (deleted) { await AppInstance.FilesystemViewModel.RemoveFileOrFolderAsync(storItem); itemsDeleted++; } else { // Stop at first error return(deleted); } } return((FilesystemResult)true); }