none
Convert VBScript to VB.Net

    Question

  • I have get a vbScript code to find RecycleBin size on local comp. but its tough for me to convert to VB.net,

    pls. hep for below said -

    Const RECYCLE_BIN = &Ha&
    Const FILE_SIZE = 3
    
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(RECYCLE_BIN)
    
    Set colItems = objFolder.Items
    
    For Each objItem in colItems
        strSize = objFolder.GetDetailsOf(objItem, FILE_SIZE)
        arrSize = Split(strSize, " ")
        intSize = intSize + CLng(arrSize(0))
    Next
    
    Wscript.Echo intSize & " KB"

    thanks.

    Thursday, January 09, 2014 3:22 AM

Answers

  • I have get a vbScript code to find RecycleBin size on local comp. but its tough for me to convert to VB.net,

    pls. hep for below said -

    Const RECYCLE_BIN = &Ha&
    Const FILE_SIZE = 3
    
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(RECYCLE_BIN)
    
    Set colItems = objFolder.Items
    
    For Each objItem in colItems
        strSize = objFolder.GetDetailsOf(objItem, FILE_SIZE)
        arrSize = Split(strSize, " ")
        intSize = intSize + CLng(arrSize(0))
    Next
    
    Wscript.Echo intSize & " KB"

    thanks.

    Why try converting VB script to VB.Net? Why not just search using a search engine to find how to get that information using VB.Net?

    Also it appears each user can have a different sized Recycle Bin on Windows 7 64 bit.

    The registry key, shown below because when I copied it here it would remove part of the last key, contains a "Name" MaxCapacity which has a hex value of 33f4. When converted to decimal 33f4 is 13300 which in my Recycle Bins properties the custom size is set to 13300 megabytes.

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume\{9d7169f3-7818-11df-8dc7-806e6f6e6963}

    So why not just get the value from the registry?

    Option Strict On
    
    Imports Microsoft.Win32
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
            Dim rk As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume", True)
            Dim SubKeyName As String = ""
            For Each Item As String In rk.GetSubKeyNames()
                SubKeyName = Item
            Next
            rk = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume\" & SubKeyName, True)
            Label1.Text = CStr((rk.GetValue("MaxCapacity", "Failed", RegistryValueOptions.DoNotExpandEnvironmentNames)))
            rk.Close()
        End Sub
    
    End Class


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    Thursday, January 09, 2014 4:19 AM
  • See the below link for an example of how to get the size of the Recycle Bin in Visual Basic .NET:

    http://www.vbforums.com/showthread.php?682275-Any-way-to-get-recycle-bin-size


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, January 09, 2014 1:31 PM
  • Yes, the GetRecycleBinCount function will fetch the count and size of the Recycle Bin. Below is the code:

    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Private Const SHERB_NOCONFIRMATION As Long = &H1
        Private Const SHERB_NOPROGRESSUI As Long = &H2
        Private Const SHERB_NOSOUND As Long = &H4
    
    
        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
    
        Private Structure details
            Dim numberOFitems As Integer
            Dim itemsSize As String
        End Structure
    
    
        Private Declare Auto Function SHEmptyRecycleBin Lib "shell32.dll" Alias "SHEmptyRecycleBinA" (ByVal hwnd As Integer, ByVal pszRootPath As String, ByVal dwFlags As Integer) As Long
    
        Private Declare Auto Function SHQueryRecycleBin Lib "shell32.dll" Alias "SHQueryRecycleBinA" (ByVal pszRootPath As String, ByRef pSHQueryRBInfo As SHQUERYRBINFO) As Long
    
        Private Declare Auto Function SHUpdateRecycleBinIcon Lib "shell32.dll" () As Long
    
        Private Sub EmptyRecycleBin()
    
            SHEmptyRecycleBin(Me.Handle.ToInt32, "", SHERB_NOCONFIRMATION)
            SHUpdateRecycleBinIcon()
    
        End Sub
    
        Private Function GetRecycleBinCount() As details
            Dim d As New details
    
            Try
                Dim SHQBI As SHQUERYRBINFO
                SHQBI.cbSize = Marshal.SizeOf(SHQBI)
                SHQueryRecycleBin("", SHQBI) ' this is the line the System.AccessViolationException occurs
                d.numberOFitems = SHQBI.i64NumItems.LowPart
                If SHQBI.i64Size.LowPart >= 1048576 Then
                    d.itemsSize = CType(Format((SHQBI.i64Size.LowPart / (1024 * 1024)), "###,###,###"), String) & " mb"
                ElseIf SHQBI.i64Size.LowPart >= 1024 Then
                    d.itemsSize = CType(Format((SHQBI.i64Size.LowPart / 1024), "###,###,###"), String) & " kb"
                Else
                    d.itemsSize = SHQBI.i64Size.LowPart & " bytes"
                End If
                Return d
            Catch exception As Exception
                MessageBox.Show(exception.ToString)
                Return Nothing
            End Try
    
        End Function
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
            lblItems.Text = GetRecycleBinCount.numberOFitems & " items"
            lblSize.Text = GetRecycleBinCount.itemsSize
        End Sub
    
        Private Sub btnEmpty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmpty.Click
            EmptyRecycleBin()
        End Sub
    
        Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click
            'open Recycle Bin
            Process.Start("explorer.exe", "/n, ::{645FF040-5081-101B-9F08-00AA002F954E}")
        End Sub
    
    End Class


    Paul ~~~~ Microsoft MVP (Visual Basic)


    Friday, January 10, 2014 4:09 AM
  • Hi, please refer the article that shows the detailed info about converting vbs to vb.net:

    http://www.codeproject.com/Articles/287277/Converting-VB-Script-To-VB-Net

    hope it helps.

    Friday, January 10, 2014 2:53 AM
  • Imports Shell32   'Reference Microsoft Shell Controls and Automation on the COM tab.
    Module Module1
      
    Const RECYCLE_BIN As Integer = &HA&
      
    Const FILE_SIZE As Integer = 3
      
    Sub Main()
        
    Dim objShell As New Shell
        
    Dim objFolder As Folder = objShell.NameSpace(RECYCLE_BIN)
        
    Dim colItems As FolderItems = objFolder.Items
        
    Dim intSize As Long
        
    For Each objItem As FolderItem In colItems
          
    Dim strSize As String = objFolder.GetDetailsOf(objItem, FILE_SIZE)
          
    Dim arrSize() As String = Split(strSize, " ")
          intSize += 
    CLng(arrSize(0))
        
    Next
        
    Console.WriteLine("Size = " + intSize.ToString + " KB")
        
    Console.ReadLine()
      
    End Sub
    End Module


    Monday, January 13, 2014 4:28 PM
  • If you go into control panel, Appearance and Personalization, Folder Options, View and uncheck "Hide protected operating system files (Recommended) then path with Windows Explorer to "C:\$Recycle.Bin" you will see a folder called "Recycle Bin" and some subfolders listed by security identifiers (SIDs).

    The folder Recycle Bin, on my desktop, contains one file called "Delete this" which contains the text "Hello". I'm restricted from accessing any of the sub folders in the path "C:\$Recycle.Bin" except the sub folder Recycle Bin using Windows Explorer. The sub folder Recycle Bin shows one deleted item in it which is "Delete this" a 5 byte text document.

    Note in the first image below the first picture displays the location "C:\$Recycle.Bin\Recycle Bin". However when I click on the arrow to the right of the path, in the second picture below, the location changes to "C:\$Recycle.Bin\S-1-5-21-380765377-4082563730-3099524409-1000" which is the SID for my user.

    So the code I used accesses each sub folder in ""C:\$Recycle.Bin" except the sub folder Recycle Bin and displays their paths in ListBox1. ListBox2 displays the SIDs and value "ProfileImagePath" for each SID. Note that I and the guest user are listed as well as other system users I would guess. However some of the SID's shown in ListBox1 do not match some of the SIDs shown in ListBox2 for some reason. See the first picture in the second image.

    If I click in ListBox1 on a path I get a file count for the path and a list of files for that path in RichTextBox1. See the second picture in the second image. Note that no matter which path you click on they all contain at least one file called "desktop.ini" for some reason.

    If I click in ListBox1 on the path that contains my SID all the sudden 92 path/filenames appear. Even though my Recycle Bin on my desktop shows only one file for some reason. See the third picture in the second image.

    I have not tried deleting any of the files contained in any of those SID sub folders to see what happens and do not plan to do so until I study up on it some more. Especially since some of the SIDs shown in ListBox1 do not match SIDs from the registry for some reason. Perhaps I should just delete those but I do not know.

    But the file "C:\$Recycle.Bin\S-1-5-21-380765377-4082563730-3099524409-1000\$RIDYO8J.txt" is the file "Delete this" as I read its contents which was "Hello".

    Option Strict On
    
    Imports System
    Imports System.Security.Principal
    Imports System.Collections.Generic
    Imports System.IO
    Imports System.Linq
    Imports Microsoft.Win32
    
    Public Class Form1
    
        ' Application must be run with administrative privileges
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Me.CenterToScreen()
    
            Dim rk As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume", True)
            Dim SubKeyName As String = ""
            For Each Item As String In rk.GetSubKeyNames()
                SubKeyName = Item
            Next
            rk = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume\" & SubKeyName, True)
            Label1.Text = "The Recycle Bins capacity is " & CStr((rk.GetValue("MaxCapacity", "Failed", RegistryValueOptions.DoNotExpandEnvironmentNames))) & " megabytes."
            rk.Close()
    
            Label2.Text = "Waiting"
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            ListBox1.Items.Clear()
            ListBox2.Items.Clear()
    
            Dim Path As String = "C:\$Recycle.Bin"
    
            Dim FolderPaths As List(Of String) = New List(Of String)(Directory.EnumerateDirectories(Path))
            For Each Item In FolderPaths
                ListBox1.Items.Add(Item & vbCrLf)
            Next
    
            Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", True)
    
            For Each Item As String In rk.GetSubKeyNames()
                Dim rk1 As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & Item, True)
                ListBox2.Items.Add(Item & " = " & CStr((rk1.GetValue("ProfileImagePath", "Failed", RegistryValueOptions.DoNotExpandEnvironmentNames))))
                rk1.Close()
            Next
    
            rk.Close()
    
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            RichTextBox1.Text = ""
            Label2.Text = "The file count for this directory is " & My.Computer.FileSystem.GetFiles(CStr(ListBox1.SelectedItem)).Count.ToString
            For Each Item In My.Computer.FileSystem.GetFiles(CStr(ListBox1.SelectedItem))
                RichTextBox1.AppendText(Item & vbCrLf)
            Next
        End Sub
    
    End Class


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    Monday, January 13, 2014 10:46 PM

All replies

  • I have get a vbScript code to find RecycleBin size on local comp. but its tough for me to convert to VB.net,

    pls. hep for below said -

    Const RECYCLE_BIN = &Ha&
    Const FILE_SIZE = 3
    
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(RECYCLE_BIN)
    
    Set colItems = objFolder.Items
    
    For Each objItem in colItems
        strSize = objFolder.GetDetailsOf(objItem, FILE_SIZE)
        arrSize = Split(strSize, " ")
        intSize = intSize + CLng(arrSize(0))
    Next
    
    Wscript.Echo intSize & " KB"

    thanks.

    Why try converting VB script to VB.Net? Why not just search using a search engine to find how to get that information using VB.Net?

    Also it appears each user can have a different sized Recycle Bin on Windows 7 64 bit.

    The registry key, shown below because when I copied it here it would remove part of the last key, contains a "Name" MaxCapacity which has a hex value of 33f4. When converted to decimal 33f4 is 13300 which in my Recycle Bins properties the custom size is set to 13300 megabytes.

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume\{9d7169f3-7818-11df-8dc7-806e6f6e6963}

    So why not just get the value from the registry?

    Option Strict On
    
    Imports Microsoft.Win32
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
            Dim rk As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume", True)
            Dim SubKeyName As String = ""
            For Each Item As String In rk.GetSubKeyNames()
                SubKeyName = Item
            Next
            rk = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume\" & SubKeyName, True)
            Label1.Text = CStr((rk.GetValue("MaxCapacity", "Failed", RegistryValueOptions.DoNotExpandEnvironmentNames)))
            rk.Close()
        End Sub
    
    End Class


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    Thursday, January 09, 2014 4:19 AM
  • See the below link for an example of how to get the size of the Recycle Bin in Visual Basic .NET:

    http://www.vbforums.com/showthread.php?682275-Any-way-to-get-recycle-bin-size


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, January 09, 2014 1:31 PM
  • Hi, please refer the article that shows the detailed info about converting vbs to vb.net:

    http://www.codeproject.com/Articles/287277/Converting-VB-Script-To-VB-Net

    hope it helps.

    Friday, January 10, 2014 2:53 AM
  • Paul Sir,

    is there any exact solution to get the recycle bin size using vb.net,

    pls. help, the above said ref. url is useful for empty the RB but vulgure to get the RB size,

    pls. hep sir.

    Friday, January 10, 2014 3:41 AM
  • Yes, the GetRecycleBinCount function will fetch the count and size of the Recycle Bin. Below is the code:

    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Private Const SHERB_NOCONFIRMATION As Long = &H1
        Private Const SHERB_NOPROGRESSUI As Long = &H2
        Private Const SHERB_NOSOUND As Long = &H4
    
    
        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
    
        Private Structure details
            Dim numberOFitems As Integer
            Dim itemsSize As String
        End Structure
    
    
        Private Declare Auto Function SHEmptyRecycleBin Lib "shell32.dll" Alias "SHEmptyRecycleBinA" (ByVal hwnd As Integer, ByVal pszRootPath As String, ByVal dwFlags As Integer) As Long
    
        Private Declare Auto Function SHQueryRecycleBin Lib "shell32.dll" Alias "SHQueryRecycleBinA" (ByVal pszRootPath As String, ByRef pSHQueryRBInfo As SHQUERYRBINFO) As Long
    
        Private Declare Auto Function SHUpdateRecycleBinIcon Lib "shell32.dll" () As Long
    
        Private Sub EmptyRecycleBin()
    
            SHEmptyRecycleBin(Me.Handle.ToInt32, "", SHERB_NOCONFIRMATION)
            SHUpdateRecycleBinIcon()
    
        End Sub
    
        Private Function GetRecycleBinCount() As details
            Dim d As New details
    
            Try
                Dim SHQBI As SHQUERYRBINFO
                SHQBI.cbSize = Marshal.SizeOf(SHQBI)
                SHQueryRecycleBin("", SHQBI) ' this is the line the System.AccessViolationException occurs
                d.numberOFitems = SHQBI.i64NumItems.LowPart
                If SHQBI.i64Size.LowPart >= 1048576 Then
                    d.itemsSize = CType(Format((SHQBI.i64Size.LowPart / (1024 * 1024)), "###,###,###"), String) & " mb"
                ElseIf SHQBI.i64Size.LowPart >= 1024 Then
                    d.itemsSize = CType(Format((SHQBI.i64Size.LowPart / 1024), "###,###,###"), String) & " kb"
                Else
                    d.itemsSize = SHQBI.i64Size.LowPart & " bytes"
                End If
                Return d
            Catch exception As Exception
                MessageBox.Show(exception.ToString)
                Return Nothing
            End Try
    
        End Function
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
            lblItems.Text = GetRecycleBinCount.numberOFitems & " items"
            lblSize.Text = GetRecycleBinCount.itemsSize
        End Sub
    
        Private Sub btnEmpty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmpty.Click
            EmptyRecycleBin()
        End Sub
    
        Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click
            'open Recycle Bin
            Process.Start("explorer.exe", "/n, ::{645FF040-5081-101B-9F08-00AA002F954E}")
        End Sub
    
    End Class


    Paul ~~~~ Microsoft MVP (Visual Basic)


    Friday, January 10, 2014 4:09 AM
  • Paul Sir,

    all the items are reletively good & illustrates fine -

    don't know why no. of items & size of RB not getting fetched on form startup,

    that's why i am sharing the startup screen -

    please help.

    Saturday, January 11, 2014 1:39 AM
  • What is your current code?


    remember make the reply as answer and vote the reply as helpful if it helps.

    Monday, January 13, 2014 6:33 AM
  • Can you post your code? The code I posted works fine for me.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, January 13, 2014 1:45 PM
  • Imports Shell32   'Reference Microsoft Shell Controls and Automation on the COM tab.
    Module Module1
      
    Const RECYCLE_BIN As Integer = &HA&
      
    Const FILE_SIZE As Integer = 3
      
    Sub Main()
        
    Dim objShell As New Shell
        
    Dim objFolder As Folder = objShell.NameSpace(RECYCLE_BIN)
        
    Dim colItems As FolderItems = objFolder.Items
        
    Dim intSize As Long
        
    For Each objItem As FolderItem In colItems
          
    Dim strSize As String = objFolder.GetDetailsOf(objItem, FILE_SIZE)
          
    Dim arrSize() As String = Split(strSize, " ")
          intSize += 
    CLng(arrSize(0))
        
    Next
        
    Console.WriteLine("Size = " + intSize.ToString + " KB")
        
    Console.ReadLine()
      
    End Sub
    End Module


    Monday, January 13, 2014 4:28 PM
  • If you go into control panel, Appearance and Personalization, Folder Options, View and uncheck "Hide protected operating system files (Recommended) then path with Windows Explorer to "C:\$Recycle.Bin" you will see a folder called "Recycle Bin" and some subfolders listed by security identifiers (SIDs).

    The folder Recycle Bin, on my desktop, contains one file called "Delete this" which contains the text "Hello". I'm restricted from accessing any of the sub folders in the path "C:\$Recycle.Bin" except the sub folder Recycle Bin using Windows Explorer. The sub folder Recycle Bin shows one deleted item in it which is "Delete this" a 5 byte text document.

    Note in the first image below the first picture displays the location "C:\$Recycle.Bin\Recycle Bin". However when I click on the arrow to the right of the path, in the second picture below, the location changes to "C:\$Recycle.Bin\S-1-5-21-380765377-4082563730-3099524409-1000" which is the SID for my user.

    So the code I used accesses each sub folder in ""C:\$Recycle.Bin" except the sub folder Recycle Bin and displays their paths in ListBox1. ListBox2 displays the SIDs and value "ProfileImagePath" for each SID. Note that I and the guest user are listed as well as other system users I would guess. However some of the SID's shown in ListBox1 do not match some of the SIDs shown in ListBox2 for some reason. See the first picture in the second image.

    If I click in ListBox1 on a path I get a file count for the path and a list of files for that path in RichTextBox1. See the second picture in the second image. Note that no matter which path you click on they all contain at least one file called "desktop.ini" for some reason.

    If I click in ListBox1 on the path that contains my SID all the sudden 92 path/filenames appear. Even though my Recycle Bin on my desktop shows only one file for some reason. See the third picture in the second image.

    I have not tried deleting any of the files contained in any of those SID sub folders to see what happens and do not plan to do so until I study up on it some more. Especially since some of the SIDs shown in ListBox1 do not match SIDs from the registry for some reason. Perhaps I should just delete those but I do not know.

    But the file "C:\$Recycle.Bin\S-1-5-21-380765377-4082563730-3099524409-1000\$RIDYO8J.txt" is the file "Delete this" as I read its contents which was "Hello".

    Option Strict On
    
    Imports System
    Imports System.Security.Principal
    Imports System.Collections.Generic
    Imports System.IO
    Imports System.Linq
    Imports Microsoft.Win32
    
    Public Class Form1
    
        ' Application must be run with administrative privileges
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Me.CenterToScreen()
    
            Dim rk As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume", True)
            Dim SubKeyName As String = ""
            For Each Item As String In rk.GetSubKeyNames()
                SubKeyName = Item
            Next
            rk = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume\" & SubKeyName, True)
            Label1.Text = "The Recycle Bins capacity is " & CStr((rk.GetValue("MaxCapacity", "Failed", RegistryValueOptions.DoNotExpandEnvironmentNames))) & " megabytes."
            rk.Close()
    
            Label2.Text = "Waiting"
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            ListBox1.Items.Clear()
            ListBox2.Items.Clear()
    
            Dim Path As String = "C:\$Recycle.Bin"
    
            Dim FolderPaths As List(Of String) = New List(Of String)(Directory.EnumerateDirectories(Path))
            For Each Item In FolderPaths
                ListBox1.Items.Add(Item & vbCrLf)
            Next
    
            Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", True)
    
            For Each Item As String In rk.GetSubKeyNames()
                Dim rk1 As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & Item, True)
                ListBox2.Items.Add(Item & " = " & CStr((rk1.GetValue("ProfileImagePath", "Failed", RegistryValueOptions.DoNotExpandEnvironmentNames))))
                rk1.Close()
            Next
    
            rk.Close()
    
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            RichTextBox1.Text = ""
            Label2.Text = "The file count for this directory is " & My.Computer.FileSystem.GetFiles(CStr(ListBox1.SelectedItem)).Count.ToString
            For Each Item In My.Computer.FileSystem.GetFiles(CStr(ListBox1.SelectedItem))
                RichTextBox1.AppendText(Item & vbCrLf)
            Next
        End Sub
    
    End Class


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    Monday, January 13, 2014 10:46 PM
  • Paul sir,

    the code is similar you had suggested above, 

    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Private Const SHERB_NOCONFIRMATION As Long = &H1
        Private Const SHERB_NOPROGRESSUI As Long = &H2
        Private Const SHERB_NOSOUND As Long = &H4
    
        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
    
        Private Structure details
            Dim numberOFitems As Integer
            Dim itemsSize As String
        End Structure
    
        Private Declare Auto Function SHEmptyRecycleBin Lib "shell32.dll" Alias "SHEmptyRecycleBinA" (ByVal hwnd As Integer, ByVal pszRootPath As String, ByVal dwFlags As Integer) As Long
    
        Private Declare Auto Function SHQueryRecycleBin Lib "shell32.dll" Alias "SHQueryRecycleBinA" (ByVal pszRootPath As String, ByRef pSHQueryRBInfo As SHQUERYRBINFO) As Long
    
        Private Declare Auto Function SHUpdateRecycleBinIcon Lib "shell32.dll" () As Long
    
        Private Sub EmptyRecycleBin()
            SHEmptyRecycleBin(Me.Handle.ToInt32, "", SHERB_NOCONFIRMATION)
            SHUpdateRecycleBinIcon()
        End Sub
    
        Private Function GetRecycleBinCount() As details
            Dim d As New details
    
            Try
                Dim SHQBI As SHQUERYRBINFO
                SHQBI.cbSize = Marshal.SizeOf(SHQBI)
                SHQueryRecycleBin("", SHQBI) ' this is the line the System.AccessViolationException occurs
                d.numberOFitems = SHQBI.i64NumItems.LowPart
                If SHQBI.i64Size.LowPart >= 1048576 Then
                    d.itemsSize = CType(Format((SHQBI.i64Size.LowPart / (1024 * 1024)), "###,###,###"), String) & " mb"
                ElseIf SHQBI.i64Size.LowPart >= 1024 Then
                    d.itemsSize = CType(Format((SHQBI.i64Size.LowPart / 1024), "###,###,###"), String) & " kb"
                Else
                    d.itemsSize = SHQBI.i64Size.LowPart & " bytes"
                End If
                Return d
            Catch exception As Exception
                MessageBox.Show(exception.ToString)
                Return Nothing
            End Try
    
        End Function
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
            lblItems.Text = GetRecycleBinCount.numberOFitems & " items"
            lblSize.Text = GetRecycleBinCount.itemsSize
        End Sub
    
        Private Sub btnEmpty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmpty.Click
            EmptyRecycleBin()
        End Sub
    
        Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click
            'open Recycle Bin
            Process.Start("explorer.exe", "/n, ::{645FF040-5081-101B-9F08-00AA002F954E}")
        End Sub
    End Class
    
    thanks.
    Tuesday, January 14, 2014 2:55 PM
  • Respected John Sir,

    the code above u suggested, literally helped a lot, nearly to exact size, but not exact,

    the no. of contents founded exactly, but still having difference on size,

    screen attached,

    kindly help Sir, thanks.

    Tuesday, January 14, 2014 3:05 PM
  • I don't understand your picture.  What result do you get using your original code and what result do you get using the code I posted?
    Tuesday, January 14, 2014 3:18 PM
  • It looks like the Form Load event I copied is missing the event handler, so it never runs. Try changing it to:

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, January 14, 2014 3:45 PM
  • Paul Sir,

    i have included the code & tried with both of syntax -

    _____________________________________________________________________________

    Friend Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Friend Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    _____________________________________________________________________________

    but still geting "0 bytes",

    oh god, what i do now,

    sir, please help...

    Wednesday, January 15, 2014 1:36 AM
  • Respected John Sir,

    the result have been posted by using your code, & i am getting difference of RB
    sizes - 



    on below red box, it is the RB exact size & on msgbox i have shown the size
    i found by your code snippet -

    RB size showing inside red rectangle => 18.6 KB
    RB size by code showing on msgbox = 12022 byte => 11.7 KB

    Difference on sizes => 6.9 KB

    why it is happening, please help sir, thanks.
    Wednesday, January 15, 2014 2:02 AM
  • Respected John Sir,

    the code above u suggested, literally helped a lot, nearly to exact size, but not exact,

    the no. of contents founded exactly, but still having difference on size,

    screen attached,

    I don't understand your picture at all.  You're running a VBS script and my console application, but you show a Form and a MessageBox?  The VBS script and  my code should get the same results.

    "I have get a vbScript code to find RecycleBin size on local comp. but its tough for me to convert to VB.net,"

    I attempted to answer the question you posted in your OP in this thread.

    I changed the "KB" to "bytes" in error.  Corrected the code.

    • Edited by JohnWein Wednesday, January 15, 2014 4:37 AM
    Wednesday, January 15, 2014 3:24 AM
  • If you get "0 bytes" then that means the Recycle Bin is empty. Are you saying that it is not empty?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, January 15, 2014 4:24 AM
  • Sir,

    above i had attached snapshot for my RB size & it is not empty, still there is a junk of memory 18.6 KB,

    pls. help Paul Sir.

    Wednesday, January 15, 2014 5:43 AM
  • Which version of Windows are you using?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, January 15, 2014 1:31 PM
  • As I previously posted the Recycle Bin on my desktop contained one file. Yet the sub folder for my SID said it contained 92 files. Now my Recycle Bin is empty on my desktop and the code says it contains 91 files that total 49089 bytes even though the Recycle Bin is empty when I open it. So I changed the code in the ListBox1 selected index event to also show the byte count of all the files in the SID sub folders.

    So I suspect the code you are using does not reflect the information that the code I used appears to accurately reflect for each SID sub folder the Recycle Bin apparently uses for each user on the system.

    With the exception of the Recycle Bins size which I only retrieve for my user name. As apparently any user on the system can customize the size of the Recycle Bin for their own user name. Or maybe that sets the size for all users. As can be seen in the top image below.

    But you seem fairly intent on believing those API's information rather than reading my post and using Windows Explorer with new Control Panel settings to look at the Recycle Bin and then trying my code to see what you get.

    And the app must be run with admin privileges. Which means to run it in debugger you have to launch Visual Studio with Admin Privileges. Usually by right clicking on Visual Studio and clicking on run as admin or something when launching Visual Studio.

    Dim FilesLength As Long = 0
    
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            RichTextBox1.Text = ""
            Label2.Text = "The file count for this directory is " & My.Computer.FileSystem.GetFiles(CStr(ListBox1.SelectedItem)).Count.ToString
    
            For Each Item In My.Computer.FileSystem.GetFiles(CStr(ListBox1.SelectedItem))
                FilesLength += My.Computer.FileSystem.GetFileInfo(Item).Length
            Next
    
            RichTextBox1.AppendText("The byte count of all files in the selected folder equals " & FilesLength.ToString & " bytes." & vbCrLf & vbCrLf)
    
            For Each Item In My.Computer.FileSystem.GetFiles(CStr(ListBox1.SelectedItem))
                RichTextBox1.AppendText(Item & vbCrLf)
            Next
    
            FilesLength = 0
    
    End Sub


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.




    Wednesday, January 15, 2014 5:24 PM
  • it is MS Win8 Single Lang, Ver.: 6.2.9200.0
    Thursday, January 16, 2014 6:00 PM
  • Still awaiting....

    kindly help someone,....

    Saturday, January 18, 2014 2:43 PM
  • Still awaiting....

    kindly help someone,....

    Help to do what? Why doesn't the code I posted answer the question posed in this thread?
    Saturday, January 18, 2014 3:05 PM