3

Usually people get list of files inside the Recycle Bin using Shell32.dll.

private static IEnumerable<string> GetRecycleBinFilenames()
{
    const int ssfBitbucket = 10;
    Type t = Type.GetTypeFromProgID("Shell.Application");
    dynamic shell = Activator.CreateInstance(t);
    Folder recycleBin = shell.NameSpace(ssfBitbucket);

    foreach (FolderItem2 recfile in recycleBin.Items())
    {
        yield return recfile.Path;
    }

    Marshal.FinalReleaseComObject(shell);
}

I am mounting a VHDX file and want to get a list of files from the Recycle Bin on a mounted external disk/volume. How can I do this?

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
Brans Ds
  • 4,039
  • 35
  • 64

1 Answers1

0

As @RaymondChen suggested you can filter based on the Path property, which contains the current path of the deleted item...

foreach (FolderItem2 recfile in recycleBin.Items())
{
    if (Path.GetPathRoot(recfile.Path) == "X:\\")
        yield return recfile.Path;
}

You could also retrieve the path of the directory from which the item was deleted and filter the same way...

const int BitBucketItem_OriginalParentPath = 1;

foreach (FolderItem2 recfile in recycleBin.Items())
{
    string originalParentPath = recycleBin.GetDetailsOf(recfile, BitBucketItem_OriginalParentPath);

    if (Path.GetPathRoot(originalParentPath) == "X:\\")
        yield return recfile.Path;
}

...however if the item was deleted through a junction/symlink then originalParentPath will contain the drive of the junction/symlink, which is not necessarily the drive storing the deleted item.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68