Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a single item FROM recycle bin

Is there any way in C# (interop maybe) to delete a specific file in the recycle bin permanently?

While searching on the internet I only found ways to delete TO the recycle bin not FROM. I also don't want to empty the whole bin, just one specific file. The specific item is already in the recycle bin.

How can I do this?

EDIT:

  1. I didn't put the file there myself, nor my program. Somebody else did so I have no control over that.
  2. Windows Search somehow is able to find my file...?!?

I found out another thing, I can actually find a file in C:\RECYCLER with the same file extension but a different name. So how can I tell if that is really the file I'm looking for?

like image 820
eric Avatar asked Nov 26 '25 17:11

eric


1 Answers

You need to reference:

using Shell32;

Code:

var shl = new Shell();

// Get recycle folder
Folder recycler = shl.NameSpace(10);

FolderItems items = recycler.Items();
for (int i = 0; i < items.Count; i++)
{
    try
    {
        FolderItem fi = items.Item(i);
        string fileName = recycler.GetDetailsOf(fi, 0);
        string filePath = recycler.GetDetailsOf(fi, 1);
        string recyleDate = recycler.GetDetailsOf(fi, 2);
        
        if (fileName == "your file/folder")
        {
            // check if chosen item is a folder
            if (fi.IsFolder)
            {
                Directory.Delete(fi.Path, true);
            }
            else
            {
                File.Delete(fi.Path);
            }
        }
    }
    catch (Exception exc)
    {
        ...
    }
}

Hopefully that may be helpful. Works for me.

like image 156
Stanislav Chernychko Avatar answered Nov 29 '25 07:11

Stanislav Chernychko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!