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
 
How do you have the program startup under a different user?
 
C:\ProgramData\Microsoft\Windows\Start Menu\

I have tested the above out and the program does autostart with each user login.

I was thinking, part of my code is creating a sub-folder with the modified date of the file. I can not test this now, but could that be causing that error if the user's permission does not have rights to create c drive folders. I am very unfamiliar with this error, but it doesn't sound like a folder creation description error. But what do I know.
 
Is is the same machine and Windows version, or different?
 
I originally designed the 1st draft from Windows 10, VS2015. Again, it works perfectly on the development machine as well as the Windows 7 workstations if I am logged under my Own User Account (which happens to be Admin). I get that error message when another user logs in and the program runs (they are not Admins).


So I suppose I could re-direct my question as to it being not a Coding Issue, but why would the program installation only work for the account of the user who installed it??
 
Last edited:
I didn't think admin right should be necessary for that, but you could try it. Right click program exe or shortcut and select 'run as admin'.
 
I am using InstallShield to distribute this to the workstation(s) I am trying. I installed it under a general users account and it works fine. But yet again, if someone else logs in I get the error. Is there something during the installation that I am missing that I need.

It is a fairly short straightforward code, but are there and certificates or Reg files that need to be set for each user? Sorry, as mentioned, I am more or less a hobby writer, and this stuff is a little advance since I am exploring new waters here. I normally work with .net projects on the web, not applications.
 
Not that I'm aware of, no.
 
I just tested an app with Shell32 reference run as regular user and that works fine, so admin rigths is not the issue as I can see.
 
OK i am racking my brain on this. Here is my full code. When I publish this to generate the Executable - How does that file know What User created it????? if I copy and paste that folder with the executable onto another computer with another user logged in, it doesn't work. If I run it on my own computer with me logged in, it works ?!?!? Isn't the whole idea of Building and Publishing the application to have it used elsewhere ? Can anyone this why I can't distribute this published application without errors.

Many thanks all
Public Class Form1
    Dim sw As New Stopwatch
    Dim elapsed As TimeSpan = sw.Elapsed

    Sub DeleteFilesFromRecycleBin(Folder As String)
        Dim Filename1 As String
        Try
            Dim SH As New Shell32.Shell
            Dim RecycleBin As Shell32.Folder = SH.NameSpace(Shell32.ShellSpecialFolderConstants.ssfBITBUCKET)
            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)
                    If (Not System.IO.File.Exists("c:\archive" & Filename1 & "" & Item.Name)) Then
                        My.Computer.FileSystem.CopyFile(Item.Path,
                    "c:\archive" & Filename1 & "" & Item.Name)
                    End If
                End If
            Next Item
        Catch ex As Exception
            TextBox1.Text = ex.ToString
        End Try
    End Sub

    Public Sub checkFolder(Filename1 As String)
        If (Not System.IO.Directory.Exists("c:\archive" & Filename1)) Then
            System.IO.Directory.CreateDirectory("c:\archive" & Filename1)
        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        DeleteFilesFromRecycleBin("C: \$Recycle.Bin")
        elapsed = sw.Elapsed
        Label1.Text = String.Format("{0:00}:{1:00}.{2:000}",
                              elapsed.TotalMinutes,
                              elapsed.Seconds,
                              elapsed.Milliseconds)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        sw.Start()
        Me.WindowState = FormWindowState.Minimized
        Me.Hide()
    End Sub

End Class
 
OK how is this one for a illogical problem! Somehow, I have a project folder for the above Project. If I go to the Debug folder and run the .EXE I get no error. Any change, I mean any and rebuild breaks the application again.

Can ANYONE explain to me what is Going ON! .

How is it possible if the code is untouched and I rebuild the same code, it breaks it with an error.
 
Try 'clean solution' and rebuild. Depending on your VS it may be in Build menu or Build toolbar, or you can add it there. Optionally you can delete Obj and Bin folders in project folder, then open and build project.
 
I was hopeful your deleting of folders would work, but same result. Any additional building of the project folder continues to display that same error message. If you guy can indulge the noob here. I am getting the error message described above when I run my program. Somehow though, in making multiple copies of my work, I have the same exact code in every other copy of the project, but one project folder that does not display any error (!!!!????!!!) . So this tells me it is not a coding issue. Something, somewhere has happened to all the project files/folders/directories on the builds of all the others make them not work, but who knows what?!?

Would say, ok you have a workable project folder, what's the problem? Well, I need to do additional changes to it, but any attempt to rebuilt it brings back that error message.

Seriously, is it just me, or does all of this make absolutely no sense what's so ever?


Thanks
 
Seriously, is it just me, or does all of this make absolutely no sense what's so ever?
No, it doesn't, not that I can see, although project files may get corrupted.
 
I'd like you to try something, removing the Shell reference and using late binding, this will eliminate any problems related to the interfaces defined in interop file. This a short version of your code using late-binding:
        Dim SH = CreateObject("Shell.Application")
        Const ssfBITBUCKET = 10
        Dim RecycleBin As Object = SH.NameSpace(ssfBITBUCKET)
        For Each Item As Object In RecycleBin.Items
            'use Item.Name, Item.Path etc
        Next Item
 
Back
Top