private void Worker_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i < db.Count(); i++) { FilesToMove dir = db.GetDirInfo(i); while (dir.Count() > 0) { if (worker.CancellationPending == true) { e.Cancel = true; return; } string file = dir.GetFile(0); //The file to move string destination = dir.GetDirectory() + "\\" + Path.GetFileName(dir.GetFile(0)); //The destination folder try { db.MoveFile(file, i); } catch { } IntCurrentFile++; int prog = Convert.ToInt32(((double)IntCurrentFile / (double)db.IntTotalFiles) * 100); (sender as BackgroundWorker).ReportProgress(prog, IntCurrentFile); //pass along the actual progress as well as the numerical amount of files that have been moved Thread.Sleep(300); } } }
private void MoveHandler(object sender, RoutedEventArgs e) { if (db.Count() == 0) //TODO: Refactor so any and all GUI calls are handled elsewhere { MessageBox.Show("No files to move."); return; } MessageBoxResult result = MessageBox.Show("Are you sure you want to move the files?", "Move all files?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No); if (result == MessageBoxResult.No) { return; } bool OverwriteAll = false; bool NoToAll = false; for (int i = 0; i < db.Count(); i++) //we initially check for duplicate files or invalid directories { FilesToMove dir = db.GetDirInfo(i); List <string> removedFiles = new List <string>(); if (!Directory.Exists(db.GetDirInfo(i).GetDirectory())) { MessageBox.Show("Error: directory " + dir.GetDirectory() + " not found. Was this from an old save?", "Error", MessageBoxButton.OK, MessageBoxImage.Error); db.RemoveDirectory(i); continue; } foreach (string file in dir.GetFiles()) { if (!File.Exists(file)) { MessageBox.Show("Error: source file " + file + " not found. Was this from an old save?", "Error", MessageBoxButton.OK, MessageBoxImage.Error); removedFiles.Add(file); continue; } string destination = dir.GetDirectory() + "\\" + Path.GetFileName(file); if (File.Exists(destination) && !OverwriteAll) //file already exists at destination, and no overwrite flag set { FileCompare compare = new FileCompare(file, destination); compare.ShowDialog(); if (compare.RESULT == Result.YESTOALL) { OverwriteAll = true; } else if (compare.RESULT == Result.NO) { removedFiles.Add(file); } else if (compare.RESULT == Result.NOTOALL) { NoToAll = true; removedFiles.Add(file); } else if (compare.RESULT == Result.KEEPBOTH) { string ext = Path.GetExtension(file); string filename = Path.GetFileNameWithoutExtension(file); //First we need the amount of duplicates that are in the folder //Since we'll be off by one due to the original file not including a (x), we add one int offset = (Directory.GetFiles(dir.GetDirectory(), filename + " (?)" + ext)).Length + 2; destination = dir.GetDirectory() + "\\" + Path.GetFileNameWithoutExtension(file) + " (" + offset + ")" + ext; File.Move(file, Path.GetFullPath(file) + Path.GetFileName(destination)); //Renames file if (!dir.UpdateFileName(file, destination)) //Attempts to update the file name within the db { MessageBox.Show("Unexpected Error, something went wrong trying to keep both files. It may not exist.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } } else if (compare.RESULT == Result.CANCEL) { return; } } else if (File.Exists(destination) && NoToAll) { removedFiles.Add(file); } } } DisableButtons(); HidePreview(); imgPreview.Source = null; listViewDestination.ItemsSource = null; worker.RunWorkerAsync(); }