public static async Task <bool> RenameFileItemAsync(ListedItem item, string newName, IShellPage associatedInstance) { newName = item.ItemNameRaw.Replace(item.ItemName, newName, StringComparison.Ordinal); if (item.ItemNameRaw == newName || string.IsNullOrEmpty(newName)) { return(true); } ReturnResult renamed = ReturnResult.InProgress; if (item.PrimaryItemAttribute == StorageItemTypes.Folder) { renamed = await associatedInstance.FilesystemHelpers.RenameAsync(StorageHelpers.FromPathAndType(item.ItemPath, FilesystemItemType.Directory), newName, NameCollisionOption.FailIfExists, true); } else { renamed = await associatedInstance.FilesystemHelpers.RenameAsync(StorageHelpers.FromPathAndType(item.ItemPath, FilesystemItemType.File), newName, NameCollisionOption.FailIfExists, true); } if (renamed == ReturnResult.Success) { associatedInstance.NavToolbarViewModel.CanGoForward = false; return(true); } return(false); }
public static async Task <IStorageItem> CreateFileFromDialogResultTypeForResult(AddItemType itemType, ShellNewEntry itemInfo, IShellPage associatedInstance) { string currentPath = null; if (associatedInstance.SlimContentPage != null) { currentPath = associatedInstance.FilesystemViewModel.WorkingDirectory; if (App.LibraryManager.TryGetLibrary(currentPath, out var library)) { if (!library.IsEmpty && library.Folders.Count == 1) // TODO: handle libraries with multiple folders { currentPath = library.Folders.First(); } } } // Show rename dialog DynamicDialog dialog = DynamicDialogFactory.GetFor_RenameDialog(); await dialog.ShowAsync(); if (dialog.DynamicResult != DynamicDialogResult.Primary) { return(null); } // Create file based on dialog result string userInput = dialog.ViewModel.AdditionalData as string; var folderRes = await associatedInstance.FilesystemViewModel.GetFolderWithPathFromPathAsync(currentPath); var created = new FilesystemResult <(ReturnResult, IStorageItem)>((ReturnResult.Failed, null), FileSystemStatusCode.Generic); if (folderRes) { switch (itemType) { case AddItemType.Folder: userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : "NewFolder".GetLocalized(); created = await FilesystemTasks.Wrap(async() => { return(await associatedInstance.FilesystemHelpers.CreateAsync( StorageHelpers.FromPathAndType(PathNormalization.Combine(folderRes.Result.Path, userInput), FilesystemItemType.Directory), true)); }); break; case AddItemType.File: userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : itemInfo?.Name ?? "NewFile".GetLocalized(); created = await FilesystemTasks.Wrap(async() => { return(await associatedInstance.FilesystemHelpers.CreateAsync( StorageHelpers.FromPathAndType(PathNormalization.Combine(folderRes.Result.Path, userInput + itemInfo?.Extension), FilesystemItemType.File), true)); }); break; } } if (created == FileSystemStatusCode.Unauthorized) { await DialogDisplayHelper.ShowDialogAsync("AccessDenied".GetLocalized(), "AccessDeniedCreateDialog/Text".GetLocalized()); } return(created.Result.Item2); }