public static bool Restore(string OriginPath) { try { using (ShellItem SourceItem = RecycleBin.GetItemFromOriginalPath(OriginPath)) { Directory.CreateDirectory(Path.GetDirectoryName(SourceItem.Name)); if (File.Exists(SourceItem.FileSystemPath)) { File.Move(SourceItem.FileSystemPath, StorageController.GenerateUniquePath(SourceItem.Name)); } else if (Directory.Exists(SourceItem.FileSystemPath)) { Directory.Move(SourceItem.FileSystemPath, StorageController.GenerateUniquePath(SourceItem.Name)); } string ExtraInfoPath = Path.Combine(Path.GetDirectoryName(SourceItem.FileSystemPath), Path.GetFileName(SourceItem.FileSystemPath).Replace("$R", "$I")); if (File.Exists(ExtraInfoPath)) { File.Delete(ExtraInfoPath); } } return(true); } catch { return(false); } }
public void RecycleBinEnumTest() { // Setup files to delete var paths = new Stack <string>(); var sb = new StringBuilder(Kernel32.MAX_PATH); var fileContent = new string('0', 1024); for (int i = 0; i < 5; i++) { MakeFile(tempDir); } for (int i = 0; i < 5; i++) { MakeFile(tempDir2); } try { var startCount = RecycleBin.Count; // Delete files to bin RecycleBin.DeleteToRecycleBin(paths, true); // Get details TestContext.WriteLine($"cnt={RecycleBin.Count}; sz={RecycleBin.Size}"); Assert.That(RecycleBin.Count, Is.EqualTo(startCount + paths.Count)); // Restore files RecycleBin.Restore(paths.Select(p => RecycleBin.GetItemFromOriginalPath(p))); Assert.That(RecycleBin.Count, Is.EqualTo(startCount)); } finally { // Delete files completely while (paths.Count > 0) { File.Delete(paths.Pop()); } } void MakeFile(string dir) { Kernel32.GetTempFileName(dir, "tmp", 0, sb); paths.Push(sb.ToString()); File.WriteAllText(paths.Peek(), fileContent); } }