Share via


How to access the recyclebin using C#

Question

Tuesday, October 13, 2009 2:13 PM

See this code project article.

How can i do the same thing using C#?

Can you please provide me with a sample code?

بن بست معنا ندارد یا راهی خواهیم یافت یا راهی خواهیم ساخت Pleasure can be silence , After a deafening din

All replies (16)

Tuesday, October 13, 2009 5:20 PM ✅Answered | 2 votes

using System;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using Shell32; //Reference Microsoft Shell Controls And Automation on the COM tab.
using System.Runtime.InteropServices;
using Microsoft.VisualBasic.FileIO;
namespace RecyclerCS
{
  public partial class Form1 : Form
  {
    public Form1() {
      InitializeComponent();
    }
    private Shell Shl;
    private const long ssfBITBUCKET = 10;
    private const int recycleNAME = 0;
    private const int recyclePATH = 1;

    private void button1_Click(object sender, System.EventArgs e) {
      string S = "This is text in the file to be restored from the Recycle Bin.";
      string FileName = "C:\\Temp\\Text.txt";
      File.WriteAllText(FileName, S);
      Delete(FileName);
      MessageBox.Show(FileName + " has been moved to the Recycle Bin.");
      if (Restore(FileName))
        MessageBox.Show(FileName + " has been restored");
      else
        MessageBox.Show("Error");
      Marshal.FinalReleaseComObject(Shl);
    }
    private void Delete(string Item) {
      FileSystem.DeleteFile(Item, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
      //Gives the most control of dialogs.
    }
    private bool Restore(string Item) {
      Shl = new Shell();
      Folder Recycler = Shl.NameSpace(10);
      for (int i = 0; i < Recycler.Items().Count; i++) {
        FolderItem FI = Recycler.Items().Item(i);
        string FileName = Recycler.GetDetailsOf(FI, 0);
        if (Path.GetExtension(FileName) == "") FileName += Path.GetExtension(FI.Path);
        //Necessary for systems with hidden file extensions.
        string FilePath = Recycler.GetDetailsOf(FI, 1);
        if (Item == Path.Combine(FilePath, FileName)) {
          DoVerb(FI, "ESTORE");
          return true;
        }
      }
      return false;
    }
    private bool DoVerb(FolderItem Item, string Verb) {
      foreach (FolderItemVerb FIVerb in Item.Verbs()) {
        if (FIVerb.Name.ToUpper().Contains(Verb.ToUpper())) {
          FIVerb.DoIt();
          return true;
        }
      }
      return false;
    }
  }
}

Thursday, October 15, 2009 4:45 PM ✅Answered

Any of the verbs that apply to any of the folder items in the RecycleBin.


Friday, October 16, 2009 5:37 PM ✅Answered

Run the code I posted and stop at the breakpoint that interates through the verbs.


Tuesday, October 13, 2009 2:19 PM

This is easier to do using Visual Basic. Try the following code:

Microsoft.VisualBasic.Devices.Computer theComputer = new Microsoft.VisualBasic.Devices.Computer();

if (theComputer == null || theComputer.FileSystem == null) return;

theComputer.FileSystem.DeleteFile(pathToFile, Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);

Tuesday, October 13, 2009 2:22 PM

I didn't want to delete a file and move it to recycle bin.

I wanted something like that sample.

Please see it at least!!


Don't reply if you don't know what the question is.


Tuesday, October 13, 2009 2:25 PM

Oops, sorry! When I saw access the recycle bin, I immediately thought of that code I'd written. 


Tuesday, October 13, 2009 2:29 PM

OK! Never mind. I wanted a list of files in recyclebin with their properties and original and Rec path.

Thanks anyway.

بن بست معنا ندارد یا راهی خواهیم یافت یا راهی خواهیم ساخت Pleasure can be silence , After a deafening din


Tuesday, October 13, 2009 2:35 PM

Have you tried out this article? It has similar code to access Recycle bin files.
http://www.aspfree.com/c/a/ASP.NET-Code/Using-C-empty-recycle-bin-by-Mahesh/

Thanks,
Veera Reddy


Tuesday, October 13, 2009 2:39 PM

You cannot access Recycle Bin from C# directly - you'd need PInvoke and do the same things as described in the article you've found.

The following might help:
C# does Shell, Part 1 , C# does Shell, Part 2

codevanced.net


Tuesday, October 13, 2009 3:09 PM

These are not like that(Not that powerful) but are helpful.


Use alternatives!!!


Thursday, October 15, 2009 2:51 PM

What if we didn't have the FileName?There is always a way out


Thursday, October 15, 2009 3:32 PM

What if we didn't have the FileName?


There is always a way out

I guess you wouldn't know what to do then.  The code I posted shows how to access the RecycleBin with an example showing the restoration of a deleted file.  There are other verbs that apply to the recycle bin.  Using them, the code would be similar.


Thursday, October 15, 2009 4:06 PM

Can you explain more please?

What other verbs?

There is always a way out


Friday, October 16, 2009 5:35 PM

Give me examples of other verbs.There is always a way out


Sunday, October 18, 2009 10:17 AM

TNx 

There is always a way out


Wednesday, September 26, 2012 8:55 AM

thx
finally a working example, great job