public async Task <bool> CheckDefaultSaveFolderAccess() { if (IsEmpty) { return(false); } var item = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(DefaultSaveFolder)); var res = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(DefaultSaveFolder, item)); return(res || (FilesystemResult)FolderHelpers.CheckFolderAccessWithWin32(DefaultSaveFolder)); }
public async Task <IStorageHistory> MoveAsync(IStorageItemWithPath source, string destination, NameCollisionOption collision, IProgress <float> progress, IProgress <FileSystemStatusCode> errorCode, CancellationToken cancellationToken) { if (source.Path == destination) { progress?.Report(100.0f); errorCode?.Report(FileSystemStatusCode.Success); return(null); } if (string.IsNullOrWhiteSpace(source.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 return(await CopyAsync(source, destination, collision, progress, errorCode, cancellationToken)); } if (associatedInstance.FilesystemViewModel.WorkingDirectory.StartsWith(App.AppSettings.RecycleBinPath)) { errorCode?.Report(FileSystemStatusCode.Unauthorized); progress?.Report(100.0f); // Do not paste files and folders inside the recycle bin await DialogDisplayHelper.ShowDialogAsync( "ErrorDialogThisActionCannotBeDone".GetLocalized(), "ErrorDialogUnsupportedOperation".GetLocalized()); return(null); } IStorageItem movedItem = null; //long itemSize = await FilesystemHelpers.GetItemSize(await source.ToStorageItem(associatedInstance)); if (source.ItemType == FilesystemItemType.Directory) { if (!string.IsNullOrWhiteSpace(source.Path) && Path.GetDirectoryName(destination).IsSubPathOf(source.Path)) // We check if user tried to move anything above the source.ItemPath { var destinationName = destination.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries).Last(); var sourceName = source.Path.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries).Last(); ContentDialog dialog = new ContentDialog() { Title = "ErrorDialogThisActionCannotBeDone".GetLocalized(), Content = "ErrorDialogTheDestinationFolder".GetLocalized() + " (" + destinationName + ") " + "ErrorDialogIsASubfolder".GetLocalized() + " (" + sourceName + ")", //PrimaryButtonText = "ErrorDialogSkip".GetLocalized(), CloseButtonText = "ErrorDialogCancel".GetLocalized() }; ContentDialogResult result = await dialog.ShowAsync(); if (result == ContentDialogResult.Primary) { progress?.Report(100.0f); errorCode?.Report(FileSystemStatusCode.InProgress | FileSystemStatusCode.Success); } else { progress?.Report(100.0f); errorCode?.Report(FileSystemStatusCode.InProgress | FileSystemStatusCode.Generic); } return(null); } else { var fsResult = (FilesystemResult)await Task.Run(() => NativeFileOperationsHelper.MoveFileFromApp(source.Path, destination)); if (!fsResult) { Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); var fsSourceFolder = await source.ToStorageItemResult(associatedInstance); var fsDestinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination)); fsResult = fsSourceFolder.ErrorCode | fsDestinationFolder.ErrorCode; if (fsResult) { var fsResultMove = await FilesystemTasks.Wrap(() => MoveDirectoryAsync((StorageFolder)fsSourceFolder, (StorageFolder)fsDestinationFolder, fsSourceFolder.Result.Name, collision.Convert(), true)); if (fsResultMove == FileSystemStatusCode.AlreadyExists) { progress?.Report(100.0f); errorCode?.Report(FileSystemStatusCode.AlreadyExists); return(null); } if (fsResultMove) { if (FolderHelpers.CheckFolderForHiddenAttribute(source.Path)) { // The source folder was hidden, apply hidden attribute to destination NativeFileOperationsHelper.SetFileAttribute(fsResultMove.Result.Path, FileAttributes.Hidden); } movedItem = (StorageFolder)fsResultMove; } fsResult = fsResultMove; } if (fsResult == FileSystemStatusCode.Unauthorized || fsResult == FileSystemStatusCode.ReadOnly) { fsResult = await PerformAdminOperation(new ValueSet() { { "Arguments", "FileOperation" }, { "fileop", "MoveItem" }, { "filepath", source.Path }, { "destpath", destination }, { "overwrite", collision == NameCollisionOption.ReplaceExisting } }); } } errorCode?.Report(fsResult.ErrorCode); } } else if (source.ItemType == FilesystemItemType.File) { var fsResult = (FilesystemResult)await Task.Run(() => NativeFileOperationsHelper.MoveFileFromApp(source.Path, destination)); if (!fsResult) { Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); FilesystemResult <StorageFolder> destinationResult = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination)); var sourceResult = await source.ToStorageItemResult(associatedInstance); fsResult = sourceResult.ErrorCode | destinationResult.ErrorCode; if (fsResult) { var file = (StorageFile)sourceResult; var fsResultMove = await FilesystemTasks.Wrap(() => file.MoveAsync(destinationResult.Result, Path.GetFileName(file.Name), collision).AsTask()); if (fsResultMove == FileSystemStatusCode.AlreadyExists) { progress?.Report(100.0f); errorCode?.Report(FileSystemStatusCode.AlreadyExists); return(null); } if (fsResultMove) { movedItem = file; } fsResult = fsResultMove; } if (fsResult == FileSystemStatusCode.Unauthorized || fsResult == FileSystemStatusCode.ReadOnly) { fsResult = await PerformAdminOperation(new ValueSet() { { "Arguments", "FileOperation" }, { "fileop", "MoveItem" }, { "filepath", source.Path }, { "destpath", destination }, { "overwrite", collision == NameCollisionOption.ReplaceExisting } }); } } errorCode?.Report(fsResult.ErrorCode); } if (Path.GetDirectoryName(destination) == associatedInstance.FilesystemViewModel.WorkingDirectory) { await Windows.ApplicationModel.Core.CoreApplication.MainView.DispatcherQueue.EnqueueAsync(async() => { await Task.Delay(50); // Small delay for the item to appear in the file list List <ListedItem> movedListedItems = associatedInstance.FilesystemViewModel.FilesAndFolders .Where(listedItem => destination.Contains(listedItem.ItemPath)).ToList(); if (movedListedItems.Count > 0) { itemManipulationModel.AddSelectedItems(movedListedItems); itemManipulationModel.FocusSelectedItems(); } }, Windows.System.DispatcherQueuePriority.Low); } progress?.Report(100.0f); if (collision == NameCollisionOption.ReplaceExisting) { return(null); // Cannot undo overwrite operation } var pathWithType = movedItem.FromStorageItem(destination, source.ItemType); return(new StorageHistory(FileOperationType.Move, source, pathWithType)); }
public async Task <IStorageHistory> CopyAsync(IStorageItemWithPath source, string destination, IProgress <float> progress, IProgress <FileSystemStatusCode> errorCode, CancellationToken cancellationToken) { if (associatedInstance.FilesystemViewModel.WorkingDirectory.StartsWith(App.AppSettings.RecycleBinPath)) { errorCode?.Report(FileSystemStatusCode.Unauthorized); progress?.Report(100.0f); // Do not paste files and folders inside the recycle bin await DialogDisplayHelper.ShowDialogAsync( "ErrorDialogThisActionCannotBeDone".GetLocalized(), "ErrorDialogUnsupportedOperation".GetLocalized()); return(null); } IStorageItem copiedItem = null; //long itemSize = await FilesystemHelpers.GetItemSize(await source.ToStorageItem(associatedInstance)); if (source.ItemType == FilesystemItemType.Directory) { if (!string.IsNullOrWhiteSpace(source.Path) && Path.GetDirectoryName(destination).IsSubPathOf(source.Path)) // We check if user tried to copy anything above the source.ItemPath { var destinationName = destination.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries).Last(); var sourceName = source.Path.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries).Last(); ContentDialog dialog = new ContentDialog() { Title = "ErrorDialogThisActionCannotBeDone".GetLocalized(), Content = $"{"ErrorDialogTheDestinationFolder".GetLocalized()} ({destinationName}) {"ErrorDialogIsASubfolder".GetLocalized()} (sourceName)", //PrimaryButtonText = "ErrorDialogSkip".GetLocalized(), CloseButtonText = "ErrorDialogCancel".GetLocalized() }; ContentDialogResult result = await dialog.ShowAsync(); if (result == ContentDialogResult.Primary) { progress?.Report(100.0f); errorCode?.Report(FileSystemStatusCode.InProgress | FileSystemStatusCode.Success); } else { progress?.Report(100.0f); errorCode?.Report(FileSystemStatusCode.InProgress | FileSystemStatusCode.Generic); } return(null); } else { // CopyFileFromApp only works on file not directories var fsSourceFolder = await source.ToStorageItemResult(associatedInstance); var fsDestinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination)); var fsResult = (FilesystemResult)(fsSourceFolder.ErrorCode | fsDestinationFolder.ErrorCode); if (fsResult) { var fsCopyResult = await FilesystemTasks.Wrap(() => CloneDirectoryAsync((StorageFolder)fsSourceFolder, (StorageFolder)fsDestinationFolder, fsSourceFolder.Result.Name, CreationCollisionOption.FailIfExists)); if (fsCopyResult == FileSystemStatusCode.AlreadyExists) { var ItemAlreadyExistsDialog = new ContentDialog() { Title = "ItemAlreadyExistsDialogTitle".GetLocalized(), Content = "ItemAlreadyExistsDialogContent".GetLocalized(), PrimaryButtonText = "ItemAlreadyExistsDialogPrimaryButtonText".GetLocalized(), SecondaryButtonText = "ItemAlreadyExistsDialogSecondaryButtonText".GetLocalized(), CloseButtonText = "ItemAlreadyExistsDialogCloseButtonText".GetLocalized() }; if (Interacts.Interaction.IsAnyContentDialogOpen()) { // Only a single ContentDialog can be open at any time. return(null); } ContentDialogResult result = await ItemAlreadyExistsDialog.ShowAsync(); if (result == ContentDialogResult.Primary) { fsCopyResult = await FilesystemTasks.Wrap(() => CloneDirectoryAsync((StorageFolder)fsSourceFolder, (StorageFolder)fsDestinationFolder, fsSourceFolder.Result.Name, CreationCollisionOption.GenerateUniqueName)); } else if (result == ContentDialogResult.Secondary) { fsCopyResult = await FilesystemTasks.Wrap(() => CloneDirectoryAsync((StorageFolder)fsSourceFolder, (StorageFolder)fsDestinationFolder, fsSourceFolder.Result.Name, CreationCollisionOption.ReplaceExisting)); return(null); // Cannot undo overwrite operation } else { return(null); } } if (fsCopyResult) { if (FolderHelpers.CheckFolderForHiddenAttribute(source.Path)) { // The source folder was hidden, apply hidden attribute to destination NativeFileOperationsHelper.SetFileAttribute(fsCopyResult.Result.Path, FileAttributes.Hidden); } copiedItem = (StorageFolder)fsCopyResult; } fsResult = fsCopyResult; } errorCode?.Report(fsResult.ErrorCode); if (!fsResult) { return(null); } } } else if (source.ItemType == FilesystemItemType.File) { var fsResult = (FilesystemResult)await Task.Run(() => NativeFileOperationsHelper.CopyFileFromApp(source.Path, destination, true)); if (!fsResult) { Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); FilesystemResult <StorageFolder> destinationResult = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination)); var sourceResult = await source.ToStorageItemResult(associatedInstance); fsResult = sourceResult.ErrorCode | destinationResult.ErrorCode; if (fsResult) { var file = (StorageFile)sourceResult; var fsResultCopy = await FilesystemTasks.Wrap(() => file.CopyAsync(destinationResult.Result, Path.GetFileName(file.Name), NameCollisionOption.FailIfExists).AsTask()); if (fsResultCopy == FileSystemStatusCode.AlreadyExists) { var ItemAlreadyExistsDialog = new ContentDialog() { Title = "ItemAlreadyExistsDialogTitle".GetLocalized(), Content = "ItemAlreadyExistsDialogContent".GetLocalized(), PrimaryButtonText = "ItemAlreadyExistsDialogPrimaryButtonText".GetLocalized(), SecondaryButtonText = "ItemAlreadyExistsDialogSecondaryButtonText".GetLocalized(), CloseButtonText = "ItemAlreadyExistsDialogCloseButtonText".GetLocalized() }; if (Interacts.Interaction.IsAnyContentDialogOpen()) { // Only a single ContentDialog can be open at any time. return(null); } ContentDialogResult result = await ItemAlreadyExistsDialog.ShowAsync(); if (result == ContentDialogResult.Primary) { fsResultCopy = await FilesystemTasks.Wrap(() => file.CopyAsync(destinationResult.Result, Path.GetFileName(file.Name), NameCollisionOption.GenerateUniqueName).AsTask()); } else if (result == ContentDialogResult.Secondary) { fsResultCopy = await FilesystemTasks.Wrap(() => file.CopyAsync(destinationResult.Result, Path.GetFileName(file.Name), NameCollisionOption.ReplaceExisting).AsTask()); return(null); // Cannot undo overwrite operation } else { return(null); } } if (fsResultCopy) { copiedItem = fsResultCopy.Result; } fsResult = fsResultCopy; } } errorCode?.Report(fsResult.ErrorCode); if (!fsResult) { return(null); } } if (Path.GetDirectoryName(destination) == associatedInstance.FilesystemViewModel.WorkingDirectory) { _ = Windows.ApplicationModel.Core.CoreApplication.MainView.ExecuteOnUIThreadAsync(async() => { await Task.Delay(50); // Small delay for the item to appear in the file list List <ListedItem> copiedListedItems = associatedInstance.FilesystemViewModel.FilesAndFolders .Where(listedItem => destination.Contains(listedItem.ItemPath)).ToList(); if (copiedListedItems.Count > 0) { associatedInstance.ContentPage.AddSelectedItemsOnUi(copiedListedItems); associatedInstance.ContentPage.FocusSelectedItems(); } }, Windows.UI.Core.CoreDispatcherPriority.Low); } progress?.Report(100.0f); var pathWithType = copiedItem.FromStorageItem(destination, source.ItemType); return(new StorageHistory(FileOperationType.Copy, source, pathWithType)); }
private async Task SyncLibrarySideBarItemsUI() { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { await SidebarControl.SideBarItemsSemaphore.WaitAsync(); try { SidebarControl.SideBarItems.BeginBulkOperation(); try { if (App.AppSettings.ShowLibrarySection && !SidebarControl.SideBarItems.Contains(librarySection)) { librarySection = new LocationItem() { Text = "SidebarLibraries".GetLocalized(), Section = SectionType.Library, Font = App.Current.Resources["OldFluentUIGlyphs"] as FontFamily, Glyph = "\uEC13", SelectsOnInvoked = false, ChildItems = new ObservableCollection <INavigationControlItem>() }; SidebarControl.SideBarItems.Insert(1, librarySection); libraryItems.Clear(); libraryItems.Add(AppSettings.DocumentsPath); libraryItems.Add(AppSettings.PicturesPath); libraryItems.Add(AppSettings.MusicPath); libraryItems.Add(AppSettings.VideosPath); for (int i = 0; i < libraryItems.Count(); i++) { string path = libraryItems[i]; var item = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(path)); var res = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path, item)); if (res || (FilesystemResult)FolderHelpers.CheckFolderAccessWithWin32(path)) { var locationItem = new LocationItem { Path = path, Section = SectionType.Library, Glyph = GlyphHelper.GetItemIcon(path), Font = InteractionViewModel.FontName, IsDefaultLocation = true, Text = res.Result?.DisplayName ?? Path.GetFileName(path.TrimEnd('\\')) }; librarySection.ChildItems.Insert(i, locationItem); } } } } catch (Exception) { } SidebarControl.SideBarItems.EndBulkOperation(); } finally { SidebarControl.SideBarItemsSemaphore.Release(); } }); }