Issue trying to read Recycle Bin with Shell32

nofrills

Member
Joined
Oct 14, 2015
Messages
12
Programming Experience
3-5
I have been asked to come up with a solution to copy specific files that have been deleted to the Recycle Bin. I am far from being an expert programmer, so I am more than stumped on how to resolve the error I am receiving. The strange thing is, any computer I am physically logged in as under my works username, the program works fine. When I have the program startup under a different user is when I get the message here.
VB.NET:
System.InvalidCastException: Unable to cast COM object of type 'Shell32.ShellClass' to interface type 'Shell32.IShellDispatch6'.
This operation failed because the QueryInterface call on the COM component for the interface with IID '{286E6F1B-7113-4355-9562-96B7E9D64C54}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
   at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
   at Shell32.ShellClass.NameSpace(Object vDir)
   at WindowsApplication1.Form1.DeleteFilesFromRecycleBin(String Folder, String Folder1)


Here is the problem code in my basic vb.net program:
Sub DeleteFilesFromRecycleBin(Folder As String, Folder1 As String)
 
 
        Dim Filename1 As String
 
 
        Try
            Dim picList As String() = Directory.GetFiles(Folder)
 
            Dim SH As New Shell32.Shell
            Dim RecycleBin As Shell32.Folder = SH.NameSpace(Shell32.ShellSpecialFolderConstants.ssfBITBUCKET)
 
            Dim SB As New StringBuilder
            'Loop through the Recycle Bin and get each Items Name  
            For Each Item As Shell32.FolderItem In RecycleBin.Items
 
     
                If (Item.Type) = "DAT File" Or (Item.Type) = "PH1 File" Then
 
                    Filename1 = Item.ModifyDate.ToString("MMddyyyy")
 
                    checkFolder(Filename1)
 
                    My.Computer.FileSystem.CopyFile(Item.Path,
                    "C:\archive" & Filename1 & "" & Item.Name)
 
                End If
 
            Next Item
 
 
        Catch ex As Exception
            TextBox1.Text = ex.ToString
        End Try
 
    End Sub
 
JohnH, you are a super star. I created a new project and used your code with eveythig working as it should. I appreciate all the help you, and all provided.
 

Similar threads

Back
Top