2

NOTE: To be clear, I do not want to empty the recycle bin as answered in other questions.


Using answers from other questions on both Stack Overflow and Super User I have discovered the way to get the location of one's recycle bin in C# is:

@"C:\$Recycle.Bin\" + System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString()

However, when I run the following code I am getting different files to what is actually in my recycle bin:

string location = @"C:\$Recycle.Bin\" + System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString();
foreach (string file in Directory.GetFiles(location))
{
    Console.Writeline(file);
}                

How can I correctly get all the files in my recycling bin? My needs are to access when the files were last used and to possibly then restore them.

Thanks.

Toby Smith
  • 1,505
  • 2
  • 15
  • 24

1 Answers1

2

It's not as straight forward as initially thought because the Recycle Bin doesn't have a physical location, it's a virtual folder. To get a list of all files in the Recycle Bin and make this work you need to add a reference to shell32.dll (located in %SystemRoot%\system32\shell32.dll).

public static void Main(string[] args)
{
    Shell shell = new Shell();
    Folder folder = shell.NameSpace(0x000a);

    foreach (FolderItem2 item in folder.Items())
        Console.WriteLine("FileName:{0}", item.Name);

    Marshal.FinalReleaseComObject(shell);
    Console.ReadLine();
}

REF: http://www.dreamincode.net/forums/topic/161500-play-with-recycle-bin-in-code/

To get the LastModified Property of the files you can see my answer here: https://stackoverflow.com/a/11660616/

Also note there's a Recycle Bin for each HDD so you will have to check all drives.

To restore files here is the solution in C#: https://stackoverflow.com/a/6025331/495455

Community
  • 1
  • 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Wow, thank you. Your code works perfectly and I'll test the restoring files stuff now. I'm assuming after I restore it I can keep track of it's now restored location? – Toby Smith Feb 20 '17 at 01:23
  • Yes its *new* restored filepath will be a property on the `FolderItem` object. You can save this to a database or whatever to keep track of files that have been deleted but you've detected should not have been. Just a FYI not sure what you plan on doing when users Empty the recycle bin or if they use a program to destroy evidence that the file ever existed, here is a list: https://www.howtogeek.com/72130/learn-how-to-securely-delete-files-in-windows/ – Jeremy Thompson Feb 20 '17 at 01:27
  • I'm struggling slightly because in the example they're using the location of the file from *before* it was recycled to restore it. I only have it's location in the recycle bin – Toby Smith Feb 20 '17 at 01:34
  • 1
    To get the **original location** it should be `objFolder.GetDetailsOf(item, indexes[39])` – Jeremy Thompson Feb 20 '17 at 01:41
  • Thanks again, that's just what I needed! – Toby Smith Feb 20 '17 at 01:43
  • Add [STAThread()] above the main method to get away with the exception – Shah Aadil Aug 14 '21 at 09:28