Search code examples
c#.netvb.netrecycle-binwindows-api-code-pack

How to Invoke a Verb on a ShellFile/ShellFolder/ShellObject objects using Windows API Code Pack?


I'm looking for a C# or Vb.NET solution to know how I can invoke the undelete verb on a ShellObject (ShellFile or ShellFolder) stored in the recycle bin.

The recycle bin part I've it written, what I only need is to know how to invoke a verb on a deleted item.

To understand me better I'll show this example of how I invoke a verb using the Shell32 Interface, but I can't find any similar method on the ShellObject/ShellFile/ShellFolder items of the Windows API Code Pack library where I'm interested to use:

Private SH As New Shell
Private RecycleBin As Folder = 
        SH.NameSpace(ShellSpecialFolderConstants.ssfBITBUCKET)

Dim RandomDeletedItem As FolderItem = RecycleBin.Items.Cast(Of FolderItem)(1)

RandomDeletedItem.InvokeVerb("undelete")

And this should be the Windows API Code Pack incompleted equivalent (In VB.NET)

Private Shared RecycleBin As IKnownFolder = KnownFolders.RecycleBin

Dim RandomDeletedItem As ShellObject = RecycleBin(1)
Dim RandomDeletedFile As ShellFile = RecycleBin(1)
Dim RandomDeletedFolder As ShellFolder = RecycleBin(1)

' Here I need to know how to invoke the "undelete" verb on each object above...

Solution

  • How to use ShellExecuteEx to undelete a file in the Recycling Bin given its parsing name:

        ' some file we got from the recycle bin iterating through the IShellItems:
        Dim sParsing As String = _
          "E:\$RECYCLE.BIN\S-1-5-21-2546332361-822210090-45395533-1001\$RTD2G1Z.PNG"
        Dim shex As New SHELLEXECUTEINFO
        shex.cbSize = Marshal.SizeOf(shex)
    
        ' Here we want ONLY the file name.
        shex.lpFile = Path.GetFileName(sParsing)
    
        ' Here we want the exact directory from the parsing name
        shex.lpDirectory = Path.GetDirectoryName(sParsing)
        shex.nShow = SW_SHOW  ' = 5
        '' here the verb is undelete, not restore.
        shex.lpVerb = "undelete"
    
        ShellExecuteEx(shex)