Skip to content

Instantly share code, notes, and snippets.

@berezovskyi
Forked from TJYSunset/UnrecycleThem.cs
Created August 20, 2020 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berezovskyi/6a3f2f8cd025e301a5eacd807b3893e9 to your computer and use it in GitHub Desktop.
Save berezovskyi/6a3f2f8cd025e301a5eacd807b3893e9 to your computer and use it in GitHub Desktop.
This program restores all files in your recycle bin. I wrote this because explorer failed to restore too many files. It's tested on Windows 10, Chinese Simplified and must be edited to run properly on Windows of other languages.
using System;
using System.Runtime.InteropServices;
using Shell32;
namespace UnrecycleThem
{
public class UnrecycleThem
{
public static void Main(string[] args)
{
var recycler = NameSpace(10); // Magic number
var items = recycler.Items();
var total = items.Count;
var count = 0;
Console.WriteLine($"Recycle Bin Items count: {total}");
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
for (var i = 0; i < items.Count; i++)
{
var fi = items.Item(i);
DoVerb(fi, @"还原(&E)"); // Replace this with the command shown in your explorer
count++;
Console.CursorLeft = 0;
Console.Write($"{count + 1}/{total}");
Console.Write(" "); // clear line
}
Console.WriteLine();
Console.WriteLine("Done!");
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
private static Folder NameSpace(object path) // Necessary for Windows 10
{
var shellAppType = Type.GetTypeFromProgID("Shell.Application");
var shell = Activator.CreateInstance(shellAppType);
var val = (Folder) shellAppType.InvokeMember("NameSpace",
System.Reflection.BindingFlags.InvokeMethod, null, shell, new[] {path});
Marshal.ReleaseComObject(shell);
return val;
}
private static void DoVerb(FolderItem item, string verb)
{
foreach (FolderItemVerb fiVerb in item.Verbs())
{
if (fiVerb.Name != verb) return;
fiVerb.DoIt();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment