[RESOLVED] Get the number of items or size of items in the recycle bin
I've got some old code of mine that I'm going back to and updating a little bit. On one part of it I need to check if there are any items in the recycle bin or not.
This is the code I was using to accomplish this:
vbcode Code:
Private Declare Function SHQueryRecycleBin Lib "shell32.dll" Alias "SHQueryRecycleBinA" (ByVal pszRootPath As String, ByRef pSHQueryRBInfo As SHQUERYRBINFO) As Long
Private Structure ULARGE_INTEGER
Dim LowPart As Integer
Dim HighPart As Integer
End Structure
Private Structure SHQUERYRBINFO
Dim cbSize As Integer
Dim i64Size As ULARGE_INTEGER
Dim i64NumItems As ULARGE_INTEGER
End Structure
Public Function IsRecycleBinEmpty() As Boolean
Dim SHQBI As SHQUERYRBINFO
SHQBI.cbSize = Marshal.SizeOf(SHQBI)
SHQueryRecycleBin("", SHQBI)
If (SHQBI.i64Size.LowPart <> LastRecycleBinSize) Then
MsgBox(SHQBI.i64Size.HighPart.ToString + " - " + SHQBI.i64Size.LowPart.ToString + " - " + SHQBI.i64NumItems.LowPart.ToString + " - " + SHQBI.i64NumItems.HighPart.ToString)
LastRecycleBinSize = SHQBI.i64Size.LowPart
End If
End
If (SHQBI.i64NumItems.LowPart) Then
IsRecycleBinEmpty = False
Else
IsRecycleBinEmpty = True
End If
End Function
The msgbox and end is just in there for debugging purposes. This always returns all 0s, even when I know that there is something in the recycle bin.
Anyone have any ideas on what I'm doing wrong here?
Thanks!
Re: Get the number of items or size of items in the recycle bin
Shouldn't
Code:
SHQBI.cbSize = Marshal.SizeOf(SHQBI)
Be
Code:
SHQBI.cbSize = Marshal.SizeOf(typeof(SHQUERYRBINFO))
Can't you just check CInt(SHQBI.i64NumItems) = 0 and return True/False based on that?
Here's an alternate solution that you may find helpful: http://www.dreamincode.net/forums/to...bin-with-c%23/
1 Attachment(s)
Re: Get the number of items or size of items in the recycle bin
Re: Get the number of items or size of items in the recycle bin
MattP, doing it that way just complains about SHQUERYBINFO being a type and unable to be used in an expression.
.paul., that looks just much like my code as far as I'm seeing (except you storing the variables in another custom type "details"), but mine doesn't work. What OS are you running on that it is working for you? I'm running Windows 7, and no matter how many items or what size the items are in my recycle bin, both LowPart and HighPart on the NumOfItems and Size is always 0.
Re: Get the number of items or size of items in the recycle bin
i wrote it in vb2005.
as far as i can remember it worked in xp, but definitely in vista + win7
have you tried setting your target cpu to x86?
Re: Get the number of items or size of items in the recycle bin
looks like you found the same example i found years ago:D
Re: Get the number of items or size of items in the recycle bin
Had target cpu set to any cpu. Setting it to x86 indeed works, but causes other problems in my code.
My project is a shortcut bar similar to the one that came with Microsoft Office 2000, gives me my desktop icons in a nice little bar that sits up in the title bar area. I usually hide my desktop icons for a clean desktop and use this bar for launching applications.
With the target cpu set to x86, it can't correctly fetch the icon of any specifically 64 bit compiled software. There may be other problems too, that is just the first thing that I noticed. I will play with this some more though, as one of the problems I want to fix is something I did is making it reference the actual file instead of the shortcut to the file, so if I wanted to delete just a shortcut I have to unhide my icons and delete the shortcut. The way I have it configured if I try to delete from the system menu I call upon when right clicking the icon on my bar, it will delete the actual target executable file.
But, since I prefer this to be a 64 bit compile, does anyone have a method of making this work with a x64 target cpu?
Re: Get the number of items or size of items in the recycle bin
Ok, found a way to do this while keeping my target cpu as any cpu.
Added system32/shell32.dll as a reference and then:
Code:
Public Function IsRecycleBinEmpty() As Boolean
Dim Shell As New Shell32.Shell
Dim RBin As Shell32.Folder = Shell.NameSpace(Shell32.ShellSpecialFolderConstants.ssfBITBUCKET)
If RBin.Items.Count Then
IsRecycleBinEmpty = False
Else
IsRecycleBinEmpty = True
End If
End Function
Thanks for the help guys, and thanks for the link MattP, that is where I got the idea to do it this way from.