private void MoveFolder_UpdateTreeViewFolderBrowser(TreeViewFolderBrowser folderTreeView, string sourceDirectory, string targetDirectory, TreeNode targetNode) { if (sourceDirectory == targetDirectory) { return; //Can't move into itself. No need for error message } try { using (new WaitCursor()) { FileData[] fileDatas = FastDirectoryEnumerator.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories); #region Move all folder and files Logger.Trace("Move folder from:" + sourceDirectory + " to: " + targetDirectory); System.IO.Directory.Move(sourceDirectory, targetDirectory); #endregion #region Clear ImageListView ImageListViewHandler.ClearAllAndCaches(imageListView1); #endregion #region Update node tree GlobalData.DoNotTrigger_TreeViewFolder_BeforeAndAfterSelect = true; TreeViewFolderBrowserHandler.RefreshFolderWithName(folderTreeView, sourceDirectory, true); TreeViewFolderBrowserHandler.RemoveFolderWithName(folderTreeView, sourceDirectory); TreeViewFolderBrowserHandler.RefreshFolderWithName(folderTreeView, targetDirectory, true); GlobalData.DoNotTrigger_TreeViewFolder_BeforeAndAfterSelect = false; #endregion #region Update database foreach (FileData oldFileData in fileDatas) { string oldFilename = Path.GetFileName(oldFileData.Path); string newFullFilename = Path.Combine(targetDirectory, oldFilename); Logger.Trace("Rename from:" + oldFileData.Path + " to: " + newFullFilename); databaseAndCacheThumbnailPoster.Move(Path.GetDirectoryName(oldFileData.Path), Path.GetFileName(oldFileData.Path), Path.GetDirectoryName(newFullFilename), Path.GetFileName(newFullFilename)); databaseAndCacheMetadataExiftool.Move(Path.GetDirectoryName(oldFileData.Path), Path.GetFileName(oldFileData.Path), Path.GetDirectoryName(newFullFilename), Path.GetFileName(newFullFilename)); } #endregion DirectoryInfo directoryInfo = new DirectoryInfo(sourceDirectory); string targetFullFolderName = targetDirectory + directoryInfo.Parent.Name; GlobalData.DoNotTrigger_TreeViewFolder_BeforeAndAfterSelect = true; treeViewFolderBrowser1.Populate(targetDirectory); GlobalData.DoNotTrigger_TreeViewFolder_BeforeAndAfterSelect = false; } //----- Updated ImageListView with files ------ ImageListView_FetchListOfMediaFiles_FromFolder_and_Aggregate(false, true); } catch (Exception ex) { Logger.Error(ex, "Error when move folder."); AddError( sourceDirectory, AddErrorFileSystemRegion, AddErrorFileSystemMoveFolder, sourceDirectory, targetDirectory, "Issue: Failed moving directory.\r\n" + "From Directory: " + sourceDirectory + "\r\n" + "To Directory: " + targetDirectory + "\r\n" + "Error message: " + ex.Message); } }
private void RenameFile_Thread_UpdateTreeViewFolderBrowser(TreeViewFolderBrowser folderTreeView, ImageListView imageListView, int renameQueueCount, string sourceFullFilename, string targetFullFilename) { if (InvokeRequired) { this.BeginInvoke(new Action <TreeViewFolderBrowser, ImageListView, int, string, string>(RenameFile_Thread_UpdateTreeViewFolderBrowser), folderTreeView, imageListView, renameQueueCount, sourceFullFilename, targetFullFilename); return; } try { GlobalData.DoNotTrigger_ImageListView_SelectionChanged = true; //ImageListViewHandler.SuspendLayout(imageListView1); using (new WaitCursor()) { try { bool directoryCreated = filesCutCopyPasteDrag.MoveFile(sourceFullFilename, targetFullFilename); if (directoryCreated) { GlobalData.DoNotTrigger_TreeViewFolder_BeforeAndAfterSelect = true; TreeViewFolderBrowserHandler.RefreshFolderWithName(folderTreeView, targetFullFilename, true); GlobalData.DoNotTrigger_TreeViewFolder_BeforeAndAfterSelect = false; } ImageListViewItem foundItem = ImageListViewHandler.FindItem(imageListView.Items, sourceFullFilename); if (foundItem != null) { ImageListViewHandler.ImageListViewRemoveItem(imageListView, foundItem); #region Add new renames back to list lock (keepTrackOfLoadedMetadataLock) { ImageListViewHandler.ImageListViewAddItem(imageListView1, targetFullFilename, ref hasTriggerLoadAllMetadataActions, ref keepTrackOfLoadedMetadata); } #endregion #region Select back all Items renamed foundItem = ImageListViewHandler.FindItem(imageListView.Items, targetFullFilename); if (foundItem != null) { foundItem.Selected = true; } #endregion } } catch (Exception ex) { #region Error Handling DateTime dateTimeLastWriteTime = DateTime.Now; try { dateTimeLastWriteTime = FileHandler.GetLastWriteTime(sourceFullFilename); } catch { } FileStatus fileStatus = FileHandler.GetFileStatus( sourceFullFilename, checkLockedStatus: true, hasErrorOccured: true, errorMessage: ex.Message); ImageListView_UpdateItemFileStatusInvoke(sourceFullFilename, fileStatus); FileStatus fileStatusTarget = FileHandler.GetFileStatus( targetFullFilename, checkLockedStatus: true, hasErrorOccured: true, errorMessage: ex.Message, exiftoolProcessStatus: ExiftoolProcessStatus.DoNotUpdate); ImageListView_UpdateItemFileStatusInvoke(targetFullFilename, fileStatus); AddError( Path.GetDirectoryName(sourceFullFilename), Path.GetFileName(sourceFullFilename), dateTimeLastWriteTime, AddErrorFileSystemRegion, AddErrorFileSystemMove, sourceFullFilename, targetFullFilename, "Issue: Failed moving file.\r\n" + "From File name : " + sourceFullFilename + "\r\n" + "From File staus: " + fileStatus.ToString() + "\r\n" + "To File name : " + targetFullFilename + "\r\n" + "To File staus: " + fileStatusTarget.ToString() + "\r\n" + "Error message: " + ex.Message); Logger.Error(ex, "Error when move file. From: " + sourceFullFilename + " to:" + targetFullFilename); #endregion } } } catch (Exception ex) { Logger.Error(ex); KryptonMessageBox.Show("Unexpected error occur.\r\nException message:" + ex.Message + "\r\n", "Unexpected error occur", MessageBoxButtons.OK, MessageBoxIcon.Error, showCtrlCopy: true); } finally { //ImageListViewHandler.ResumeLayout(imageListView1); GlobalData.DoNotTrigger_ImageListView_SelectionChanged = false; } if (renameQueueCount == 0) { //To avoid selected files becomes added back to read queue, and also exist in rename queue, //that rename item can get removed after rename. With old name in read queue, and this file will then not exist when read ImageListView_SelectionChanged_Action_ImageListView_DataGridView(false); } }
private void SaveRename() { if (GlobalData.IsApplicationClosing) { return; } try { if (IsFileInAnyQueueLock(imageListView1.SelectedItems)) { DataGridViewHandlerRename.RenameVaribale = Properties.Settings.Default.RenameVariable; using (new WaitCursor()) { DataGridView dataGridView = dataGridViewRename; int columnIndex = DataGridViewHandler.GetColumnIndexFirstFullFilePath(dataGridView, DataGridViewHandlerRename.headerNewFilename, false); if (columnIndex == -1) { return; } for (int rowIndex = 0; rowIndex < DataGridViewHandler.GetRowCountWithoutEditRow(dataGridView); rowIndex++) { DataGridViewGenericCell cellGridViewGenericCell = DataGridViewHandler.GetCellDataGridViewGenericCellCopy(dataGridView, columnIndex, rowIndex); if (!cellGridViewGenericCell.CellStatus.CellReadOnly) { DataGridViewGenericRow dataGridViewGenericRow = DataGridViewHandler.GetRowDataGridViewGenericRow(dataGridView, rowIndex); #region Get Old filename from grid string oldFilename = dataGridViewGenericRow.RowName; string oldDirectory = dataGridViewGenericRow.HeaderName; string oldFullFilename = FileHandler.CombinePathAndName(oldDirectory, oldFilename); #endregion AddQueueRenameMediaFilesLock(oldFullFilename, DataGridViewHandlerRename.RenameVaribale); } } } } else { Dictionary <string, string> renameSuccess; Dictionary <string, RenameToNameAndResult> renameFailed; HashSet <string> directoriesCreated; DataGridViewHandlerRename.Write(dataGridViewRename, out renameSuccess, out renameFailed, out directoriesCreated, checkBoxRenameShowFullPath.Checked); UpdateImageViewListeAfterRename(imageListView1, renameSuccess, renameFailed, true); foreach (string newDirector in directoriesCreated) { GlobalData.DoNotTrigger_TreeViewFolder_BeforeAndAfterSelect = true; TreeViewFolderBrowserHandler.RefreshFolderWithName(treeViewFolderBrowser1, newDirector, true); GlobalData.DoNotTrigger_TreeViewFolder_BeforeAndAfterSelect = false; } ImageListView_SelectionChanged_Action_ImageListView_DataGridView(false); } } catch (Exception ex) { Logger.Error(ex, "Was not able to rename files"); KryptonMessageBox.Show("Was not able to rename files.\r\n" + ex.Message, "Rename files failed.", MessageBoxButtons.OK, MessageBoxIcon.Error, showCtrlCopy: true); } }