} // /// <summary> /// Deletes all but one file from every group of matches. /// Returns the total size of deleted items. /// </summary> /// <param name="matches"></param> public long DeleteMatches( bool moveToTrash=false, MatchOrdering deleteOrder = MatchOrdering.None ) { long totalSize = 0; this.SortMatches( deleteOrder ); foreach ( FileMatchGroup match in this ) { totalSize += match.DuplicatesSize; string[] files = match.RemoveMatches(); int len = files.Length; /// don't delete the first file from each group. for ( int i = 0; i < len; i++ ) { if ( moveToTrash ) { RecycleBinDeleter.Delete( files[i] ); } else { File.Delete( files[i] ); } } // for-loop. } // for-loop. return totalSize; } //
/// <summary> /// Method performs the actual delete operation. /// </summary> private void DeleteFiles() { bool moveToTrash = this.MoveToTrash; int len = this.toDelete.Count; for (int i = 0; i < len; i++) { string path = this.toDelete[i]; try { if (moveToTrash) { if (!RecycleBinDeleter.Delete(path)) { /// recycleBindDelete could not delete. this.failedPaths.Add(path); } } else if (File.Exists(path)) { File.Delete(path); } else if (Directory.Exists(path)) { Directory.Delete(path); } } catch (Exception e) { Log(e.ToString()); this.errors.Add(e); this.failedPaths.Add(path); } this.AdvanceProgress(); } // for-loop } // DeleteFiles()
public void DeleteFileToRecycleBin(string path) { // This won't work with paths > MAX_PATH if (path.Length >= maxPath) { Pri.LongPath.File.Delete(path); } else { RecycleBinDeleter.Delete(path); } }
} // /// <summary> /// Attempts to delete a file asynchronously. /// All exceptions are caught. /// </summary> /// <param name="path"></param> /// <param name="moveToTrash"></param> public async void DeleteFileAsync(string path, bool moveToTrash = false) { try { await Task.Run(() => { if (moveToTrash) { RecycleBinDeleter.Delete(path); } else if (File.Exists(path)) { File.Delete(path); } else if (Directory.Exists(path)) { Directory.Delete(path); } } ); } catch (Exception) { } }
override public FileActionResult Run(FileSystemInfo info) { bool success = RecycleBinDeleter.Delete(info.FullName); return(new FileActionResult(success)); }
public static int Main(string[] args) { // args[4] is a new parameter containing arguments to args[3] (path to restart application) RecycleBinDeleter.Logger = s => Log("!! " + s); if (args.Length < 4) { Console.WriteLine("You should not invoke this executable directly. It is used as part of the automatic upgrade process for portable installations."); Console.ReadKey(); return(0); } var destinationPath = args[0]; var sourcePath = args[1]; var waitForPid = Int32.Parse(args[2]); // Right, so we can be called in a variety of painful ways, depending on the version of SyncTrayzor // we're upgrading from. Possible options: // 1. { 'path\to\SyncTrayzor.exe' } // 2. { 'path\to\SyncTrayzor.exe -minimized' } // 3. { 'path', 'with', 'space\to\SyncTrayzor.exe -minimized' } // 4. { 'path with space\to\SyncTrayzor.exe' } // 5. { 'path with space\to\SyncTrayzor.exe', '-minimized' } var pathToRestartParts = args.Skip(3).ToList(); var pathToRestartApplicationParameters = String.Empty; // Does the last element end with '-minimized'? Strip it off, and add it as an element at the end var lastElement = pathToRestartParts.Last(); if (lastElement.EndsWith(" -minimized")) { pathToRestartParts[pathToRestartParts.Count - 1] = lastElement.Substring(0, lastElement.Length - " -minimized".Length); pathToRestartParts.Add("-minimized"); } // Is the last element "-minimized"? Cut it off. if (pathToRestartParts.Last() == "-minimized") { pathToRestartApplicationParameters = "-minimized"; pathToRestartParts.RemoveAt(pathToRestartParts.Count - 1); } // Join the rest together var pathToRestartApplication = String.Join(" ", pathToRestartParts); var destinationPathParent = Path.GetDirectoryName(destinationPath); try { bool pauseAtEnd = false; Log("Waiting for SyncTrayzor process to exit..."); try { using (var process = Process.GetProcessById(waitForPid)) { if (!process.WaitForExit(5000)) { throw new Exception($"SyncTrayzor process with PID {waitForPid} did not exit after 5 seconds"); } } } catch (ArgumentException) // It wasn't running to start with. Coolio { } // By default our CWD is the destinationPath, which locks it try { Directory.SetCurrentDirectory(destinationPathParent); } catch (Exception) { Log($"!! Unable to set working directory to\n {destinationPathParent}.\nNone of your files have been touched."); throw; } var destinationExists = Directory.Exists(destinationPath); if (!Directory.Exists(sourcePath)) { Log($"!! Unable to find source path\n {sourcePath}.\nThis is a bug with SyncTrayzor's upgrade mechanism."); throw new Exception("Unable to find source path"); } string movedDestinationPath = null; if (destinationExists) { movedDestinationPath = GenerateBackupDestinationPath(destinationPath); while (true) { Log($"Moving\n {destinationPath}\nto\n {movedDestinationPath}"); try { DirectoryMove(destinationPath, movedDestinationPath); break; } catch (Exception e) { Log(); Log($"!! Unable to move\n {destinationPath}\nto\n {movedDestinationPath}"); Log($"Error: {e.GetType().Name} {e.Message}"); Log($"!! Please make sure that\n {destinationPath}\nor any of the files inside it, aren't open."); Log($"!! Press any key to try again, or Ctrl-C to abort the upgrade."); Log($"!! If you abort the upgrade, none of your files will be modified."); Console.ReadKey(); } } } Log($"Moving\n {sourcePath}\nto\n {destinationPath}"); try { DirectoryMove(sourcePath, destinationPath); } catch (Exception) { Log(); Log($"!! Unable to move\n {sourcePath}\nto\n {destinationPath}.\nYour copy of SyncTrayzor is at\n {sourcePath}\nand will still work."); throw; } if (destinationExists) { var sourceDataFolder = Path.Combine(movedDestinationPath, "data"); var destDataFolder = Path.Combine(destinationPath, "data"); if (Directory.Exists(sourceDataFolder)) { Log(); Log($"Copying data folder\n {sourceDataFolder}\nto\n {destDataFolder}..."); try { DirectoryCopy(sourceDataFolder, destDataFolder); } catch (Exception) { Log(); Log($"!! Unable to copy\n {sourceDataFolder}\nto\n {destDataFolder}.\nYour copy of SyncTrayzor is at\n {movedDestinationPath}\nand will still work."); throw; } } else { Log(); Log($"!! Could not find source data folder {sourceDataFolder}, so not copying. If you have ever started SyncTrayzor from {movedDestinationPath}, this is an error: please manually copy your 'data' folder from whereever it is to {destDataFolder}"); pauseAtEnd = true; } var sourceInstallCount = Path.Combine(movedDestinationPath, "InstallCount.txt"); var destInstallCount = Path.Combine(destinationPath, "InstallCount.txt"); if (File.Exists(sourceInstallCount)) { var installCount = Int32.Parse(File.ReadAllText(sourceInstallCount).Trim()); Log($"Increasing install count to {installCount + 1} from\n {sourceInstallCount}\nto\n {destInstallCount}"); try { File.WriteAllText(destInstallCount, (installCount + 1).ToString()); } catch (Exception e) { Log(); Log($"!! Unable to increase install count: {e.GetType().Name} {e.Message}. Continuing anyway."); pauseAtEnd = true; } } else { Log($"{sourceInstallCount}\ndoesn't exist, so setting installCount to 1 in\n {destInstallCount}"); try { File.WriteAllText(destInstallCount, "1"); } catch (Exception e) { Log(); Log($"!! Unable to set install count: {e.GetType().Name} {e.Message}. Continuing anyway."); pauseAtEnd = true; } } Log($"Deleting\n {movedDestinationPath}\nto the recycle bin"); try { RecycleBinDeleter.Delete(movedDestinationPath); } catch (Exception e) { Log(); Log($"!! Unable to delete your old installation at\n {movedDestinationPath}\n Error: {e.GetType().Name} {e.Message}."); Log($"Your new installation is at\n {destinationPath}\nand should be fully functional."); Log($"Please double-check, and manually delete\n {movedDestinationPath}."); pauseAtEnd = true; } } if (pauseAtEnd) { Log(); Log(); Log("One or more warnings occurred. Please review the messages above, and take any appropriate action."); WriteLogToFile(destinationPathParent); Console.WriteLine("Press any key to continue (this will restart SyncTrayzor)"); Console.ReadKey(); } Log($"Restarting application {pathToRestartApplication}"); Process.Start(pathToRestartApplication, pathToRestartApplicationParameters); return(0); } catch (Exception e) { Log(); Log($"--- An error occurred ---"); Log($"{e.GetType().Name}: {e.Message}"); Log(); Log("The upgrade failed to complete successfully. Sorry about that."); Log("Please read the messages above."); WriteLogToFile(destinationPathParent); Console.WriteLine("Press any key to continue"); Console.ReadKey(); return(2); } }