Advertisement
PiToLoKo

RecycleBin Manager for VB.NET - by Elektro

Jan 18th, 2014
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 42.42 KB | None | 0 0
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Created  : 01-17-2014
  4. ' Modified : 01-19-2014
  5. ' ***********************************************************************
  6. ' <copyright file="RecycleBinManager.vb" company="Elektro Studios">
  7. '     Copyright (c) Elektro Studios. All rights reserved.
  8. ' </copyright>
  9. ' <summary></summary>
  10. ' ***********************************************************************
  11.  
  12. ' Instructions
  13. ' 1. Add a reference to 'Microsoft.WindowsAPICodePack.dll'.
  14. ' 2. Add a reference to 'Microsoft.WindowsAPICodePack.Shell.dll'.
  15.  
  16. #Region " Public Members Summary "
  17.  
  18. ' ----------
  19. ' Properties
  20. ' ----------
  21. '
  22. ' MainBin.Files
  23. ' MainBin.Folders
  24. ' MainBin.Items
  25. ' MainBin.ItemsCount
  26. ' MainBin.LastDeletedFile
  27. ' MainBin.LastDeletedFolder
  28. ' MainBin.LastDeletedItem
  29. ' MainBin.Size
  30.  
  31. ' -------
  32. ' Methods
  33. ' -------
  34. '
  35. ' MainBin.Empty()
  36. ' MainBin.RefreshIcon()
  37. '
  38. ' Tools.Empty()
  39. ' Tools.GetSize()
  40. ' Tools.GetDeletedFiles()
  41. ' Tools.GetDeletedFolders()
  42. ' Tools.GetDeletedItems()
  43. ' Tools.GetItemsCount()
  44. ' Tools.GetLastDeletedFile()
  45. ' Tools.GetLastDeletedFolder()
  46. ' Tools.GetLastDeletedItem()
  47. ' Tools.DeleteItem
  48. ' Tools.UndeleteItem
  49. ' Tools.InvokeItemVerb
  50.  
  51. #End Region
  52.  
  53. #Region " Usage Examples "
  54.  
  55. #Region " Example 1 "
  56.  
  57. '' Empties all the Recycle Bins.
  58. 'RecycleBinManager.MainBin.Empty()
  59.  
  60. '' Empties the Recycle Bin of the "E" drive.
  61. 'RecycleBinManager.Tools.Empty("E", RecycleBinManager.Tools.RecycleBinFlags.DontShowConfirmation)
  62.  
  63. '' Updates the Main Recycle Bin icon.
  64. 'RecycleBinManager.MainBin.RefreshIcon()
  65.  
  66.  
  67. '' Gets the accumulated size (in bytes) of the Main Recycle Bin.
  68. 'Dim RecycledSize As Long = RecycleBinManager.MainBin.Size
  69.  
  70. '' Gets the accumulated size (in bytes) of the Recycle Bin on "E" drive.
  71. 'Dim RecycledSizeE As Long = RecycleBinManager.Tools.GetSize("E")
  72.  
  73.  
  74. '' Gets the total deleted items count of the Main recycle bin.
  75. 'Dim RecycledItemsCount As Long = RecycleBinManager.MainBin.ItemsCount
  76.  
  77. '' Gets the total deleted items count of the Recycle Bin on "E" drive.
  78. 'Dim RecycledItemsCountE As Long = RecycleBinManager.Tools.GetDeletedItems("E").Count
  79.  
  80.  
  81. '' Get all the deleted items inside the Main Recycle Bin.
  82. 'Dim RecycledItems As ShellObject() = RecycleBinManager.MainBin.Items
  83.  
  84. '' Get all the deleted files inside the Main Recycle Bin.
  85. 'Dim RecycledFiles As ShellFile() = RecycleBinManager.MainBin.Files
  86.  
  87. '' Get all the deleted folders inside the Main Recycle Bin.
  88. 'Dim RecycledFolders As ShellFolder() = RecycleBinManager.MainBin.Folders
  89.  
  90.  
  91. '' Get all the deleted items inside the Recycle Bin on "E" drive.
  92. 'Dim RecycledItemsE As ShellObject() = RecycleBinManager.Tools.GetDeletedItems("E")
  93.  
  94. '' Get all the deleted files inside the Recycle Bin on "E" drive.
  95. 'Dim RecycledFilesE As ShellFile() = RecycleBinManager.Tools.GetDeletedFiles("E")
  96.  
  97. '' Get all the deleted folders inside the Recycle Bin on "E" drive.
  98. 'Dim RecycledFoldersE As ShellFolder() = RecycleBinManager.Tools.GetDeletedFolders("E")
  99.  
  100.  
  101. '' Gets the Last deleted Item inside the Main Recycle Bin.
  102. 'MsgBox(RecycleBinManager.MainBin.LastDeletedItem.Name)
  103.  
  104. '' Gets the Last deleted Item inside the Recycle Bin on "E" drive
  105. 'MsgBox(RecycleBinManager.Tools.GetLastDeletedItem("E").Name)
  106.  
  107.  
  108. '' Undeletes an item.
  109. 'RecycleBinManager.Tools.UndeleteItem(RecycleBinManager.MainBin.LastDeletedItem)
  110.  
  111. '' Permanently deletes an item.
  112. 'RecycleBinManager.Tools.DeleteItem(RecycleBinManager.MainBin.LastDeletedItem)
  113.  
  114. '' Invokes an Item-Verb
  115. 'RecycleBinManager.Tools.InvokeItemVerb(RecycleBinManager.MainBin.LastDeletedItem, "properties")
  116.  
  117. #End Region
  118.  
  119. #Region " Example 2 "
  120.  
  121. 'Private Sub Test() Handles MyBase.Shown
  122.  
  123. '    Dim sb As New System.Text.StringBuilder
  124.  
  125. '    ' Get all the deleted items inside all the Recycle Bins.
  126. '    Dim RecycledItems As ShellObject() = RecycleBinManager.MainBin.Items
  127.  
  128. '    ' Loop through the deleted Items (Ordered by las deleted).
  129. '    For Each Item As ShellFile In (From itm In RecycledItems
  130. '                                   Order By itm.Properties.GetProperty("System.Recycle.DateDeleted").ValueAsObject
  131. '                                   Descending)
  132.  
  133. '        ' Append the property bags information.
  134. '        sb.AppendLine(String.Format("Full Name....: {0}",
  135. '                                    Item.Name))
  136.  
  137. '        sb.AppendLine(String.Format("Item Name....: {0}",
  138. '                                    Item.Properties.System.ItemNameDisplay.Value))
  139.  
  140. '        sb.AppendLine(String.Format("Deleted From.: {0}",
  141. '                                    Item.Properties.GetProperty("System.Recycle.DeletedFrom").ValueAsObject))
  142.  
  143. '        sb.AppendLine(String.Format("Item Type....: {0}",
  144. '                                   Item.Properties.System.ItemTypeText.Value))
  145.  
  146. '        sb.AppendLine(String.Format("Item Size....: {0}",
  147. '                                    CStr(Item.Properties.System.Size.Value)))
  148.  
  149. '        sb.AppendLine(String.Format("Attributes...: {0}",
  150. '                                    [Enum].Parse(GetType(IO.FileAttributes),
  151. '                                                 Item.Properties.System.FileAttributes.Value).ToString))
  152.  
  153. '        sb.AppendLine(String.Format("Date Deleted.: {0}",
  154. '                                    Item.Properties.GetProperty("System.Recycle.DateDeleted").ValueAsObject))
  155.  
  156. '        sb.AppendLine(String.Format("Date Modified: {0}",
  157. '                                    CStr(Item.Properties.System.DateModified.Value)))
  158.  
  159. '        sb.AppendLine(String.Format("Date Created.: {0}",
  160. '                                    CStr(Item.Properties.System.DateCreated.Value)))
  161.  
  162. '        MsgBox(sb.ToString)
  163. '        sb.Clear()
  164.  
  165. '    Next Item
  166.  
  167. 'End Sub
  168.  
  169. #End Region
  170.  
  171. #End Region
  172.  
  173. #Region " Imports "
  174.  
  175. Imports System.ComponentModel
  176. Imports System.Runtime.InteropServices
  177. Imports System.Text
  178. Imports Microsoft.WindowsAPICodePack.Shell
  179.  
  180. #End Region
  181.  
  182. ''' <summary>
  183. ''' Retrieves Recycle Bin's information and performs other operations.
  184. ''' </summary>
  185. Public NotInheritable Class RecycleBinManager
  186.  
  187. #Region " Recycle Bin Folder "
  188.  
  189.     ''' <summary>
  190.     ''' The Recycle Bin virtual folder object.
  191.     ''' </summary>
  192.     Private Shared ReadOnly RecycleBin As IKnownFolder = KnownFolders.RecycleBin
  193.  
  194. #End Region
  195.  
  196. #Region " API "
  197.  
  198.     ''' <summary>
  199.     ''' WinAPI things.
  200.     ''' </summary>
  201.     Private NotInheritable Class NativeMethods
  202.  
  203. #Region " Methods "
  204.  
  205.         ''' <summary>
  206.         ''' Empties the Recycle Bin on the specified drive.
  207.         ''' </summary>
  208.         ''' <param name="hWnd">A handle to the parent window of any dialog boxes that might be displayed during the operation.
  209.         ''' This parameter can be NULL.</param>
  210.         ''' <param name="RootPath">The address of a null-terminated string of maximum length MAX_PATH that contains the path of the
  211.         ''' root drive on which the Recycle Bin is located.
  212.         ''' This parameter can contain the address of a string formatted with the drive, folder, and subfolder names,
  213.         ''' for example 'c:\windows\system\'.
  214.         ''' It can also contain an empty string or NULL.
  215.         ''' If this value is an empty string or NULL, all Recycle Bins on all drives will be emptied.</param>
  216.         ''' <param name="Flags">Specifies the Recycle bin behavior, this parameter can be one or more flags.</param>
  217.         ''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns>
  218.         <DllImport("shell32.dll", EntryPoint:="SHEmptyRecycleBin", CharSet:=CharSet.Unicode)>
  219.         Public Shared Function EmptyRecycleBin(
  220.                       Optional ByVal hWnd As IntPtr = Nothing,
  221.                       Optional ByVal RootPath As String = Nothing,
  222.                       Optional ByVal Flags As Tools.RecycleBinFlags = Nothing
  223.         ) As Boolean
  224.         End Function
  225.  
  226.         ''' <summary>
  227.         ''' Updates the Recycle Bin icon on the desktop to reflect the state of the systemwide Recycle Bin,
  228.         ''' for example if an error occurs during an 'EmptyRecycleBin' operation.
  229.         ''' But since the other Recycle Bin management functions will update this icon on their own,
  230.         ''' there's almost no reason why your applications would need to call this function explicitly.
  231.         ''' </summary>
  232.         ''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns>
  233.         <DllImport("shell32.dll", EntryPoint:="SHUpdateRecycleBinIcon")>
  234.         Public Shared Function UpdateRecycleBinIcon(
  235.         ) As Boolean
  236.         End Function
  237.  
  238.         ''' <summary>
  239.         ''' Retrieves the accumulated size of the Recycle Bin and the number of items in it, for a specified drive.
  240.         ''' </summary>
  241.         ''' <param name="RootPath">The address of a null-terminated string of maximum length MAX_PATH to contain the path of the
  242.         ''' root drive on which the Recycle Bin is located.
  243.         ''' This parameter can contain the address of a string formatted with the drive, folder,
  244.         ''' and subfolder names (C:\Windows\System32\).</param>
  245.         ''' <param name="QueryRBInfo">The address of a SHQUERYRBINFO structure that receives the Recycle Bin information.
  246.         ''' The 'StructSize' member of the structure must be set to the size of the structure before calling this API.</param>
  247.         ''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns>
  248.         <DllImport("shell32.dll", EntryPoint:="SHQueryRecycleBin", CharSet:=CharSet.Unicode)>
  249.         Public Shared Function QueryRecycleBin(
  250.                       ByVal RootPath As String,
  251.                       ByRef QueryRBInfo As SHQUERYRBINFO
  252.         ) As Boolean
  253.         End Function
  254.  
  255.         ''' <summary>
  256.         ''' Determines the number of items in the specified menu.
  257.         ''' </summary>
  258.         ''' <param name="hMenu">A handle to the menu to be examined.</param>
  259.         ''' <returns>
  260.         ''' If the function succeeds, the return value specifies the number of items in the menu.
  261.         ''' If the function fails, the return value is -1.</returns>
  262.         <DllImport("user32.dll")>
  263.         Public Shared Function GetMenuItemCount(
  264.                   ByVal hMenu As IntPtr
  265.         ) As Integer
  266.         End Function
  267.  
  268.         ''' <summary>
  269.         ''' Retrieves the menu item identifier of a menu item located at the specified position in a menu.
  270.         ''' </summary>
  271.         ''' <param name="hMenu">
  272.         ''' A handle to the menu that contains the item whose identifier is to be retrieved.
  273.         ''' </param>
  274.         ''' <param name="nPos">
  275.         ''' The zero-based relative position of the menu item whose identifier is to be retrieved.
  276.         ''' </param>
  277.         ''' <returns>
  278.         ''' The return value is the identifier of the specified menu item.
  279.         ''' If the menu item identifier is NULL or if the specified item opens a submenu, the return value is -1.
  280.         ''' </returns>
  281.         <DllImport("user32.dll")> _
  282.         Public Shared Function GetMenuItemID(
  283.                   ByVal hMenu As IntPtr,
  284.                   ByVal nPos As Integer
  285.         ) As Integer
  286.         End Function
  287.  
  288.         ''' <summary>
  289.         ''' Creates and initializes a Shell item object from a parsing name.
  290.         ''' </summary>
  291.         ''' <param name="path">
  292.         ''' A pointer to a display name.
  293.         ''' </param>
  294.         ''' <param name="pbc">
  295.         ''' Optional.
  296.         ''' A pointer to a bind context used to pass parameters as inputs and outputs to the parsing function.
  297.         ''' These passed parameters are often specific to the data source and are documented by the data source owners.
  298.         ''' </param>
  299.         ''' <param name="riid">
  300.         ''' A reference to the IID of the interface to retrieve through ppv,
  301.         ''' typically IID_IShellItem or IID_IShellItem2.
  302.         ''' </param>
  303.         ''' <param name="item">
  304.         ''' When this method returns successfully, contains the interface pointer requested in riid.
  305.         ''' This is typically IShellItem or IShellItem2.</param>
  306.         ''' <returns>System.Int32.</returns>
  307.         <DllImport("shell32.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
  308.         Public Shared Function SHCreateItemFromParsingName(
  309.                   ByVal path As String,
  310.                   ByVal pbc As IntPtr, <MarshalAs(UnmanagedType.LPStruct)>
  311.                   ByVal riid As Guid,
  312.                   ByRef item As IShellItem
  313.         ) As Integer
  314.         End Function
  315.  
  316. #End Region
  317.  
  318. #Region " Interfaces "
  319.  
  320.         ''' <summary>
  321.         ''' Exposes methods that retrieve information about a Shell item.
  322.         ''' IShellItem and IShellItem2 are the preferred representations of items in any new code.
  323.         ''' </summary>
  324.         <ComImport, Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
  325.         Interface IShellItem
  326.  
  327.             REM NOTE: This interface is only partially defined, don't use it in other contexts.
  328.  
  329.             ''' <summary>
  330.             ''' Binds to a handler for an item as specified by the handler ID value (BHID).
  331.             ''' </summary>
  332.             ''' <param name="pbc">
  333.             ''' A pointer to an IBindCtx interface on a bind context object.
  334.             ''' Used to pass optional parameters to the handler.
  335.             ''' The contents of the bind context are handler-specific.
  336.             ''' For example, when binding to BHID_Stream, the STGM flags in the bind context indicate the
  337.             ''' mode of access desired (read or read/write).</param>
  338.             ''' <param name="bhid">
  339.             ''' Reference to a GUID that specifies which handler will be created.
  340.             ''' One of the following values defined in Shlguid.h.</param>
  341.             ''' <param name="riid">
  342.             ''' IID of the object type to retrieve.
  343.             ''' </param>
  344.             ''' <param name="ppv">
  345.             ''' When this method returns,
  346.             ''' contains a pointer of type riid that is returned by the handler specified by rbhid.
  347.             ''' </param>
  348.             ''' <returns>System.Int32.</returns>
  349.             <PreserveSig>
  350.             Function BindToHandler(
  351.                  ByVal pbc As IntPtr,
  352.                  <MarshalAs(UnmanagedType.LPStruct)> ByVal bhid As Guid,
  353.                  <MarshalAs(UnmanagedType.LPStruct)> ByVal riid As Guid,
  354.                  ByRef ppv As IContextMenu
  355.         ) As Integer
  356.         End Interface
  357.  
  358.         ''' <summary>
  359.         ''' Exposes methods that either create or merge a shortcut menu associated with a Shell object.
  360.         ''' </summary>
  361.         <ComImport, Guid("000214e4-0000-0000-c000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
  362.         Interface IContextMenu
  363.  
  364.             ''' <summary>
  365.             ''' Adds commands to a shortcut menu.
  366.             ''' </summary>
  367.             ''' <param name="hmenu">
  368.             ''' A handle to the shortcut menu.
  369.             ''' The handler should specify this handle when adding menu items.
  370.             ''' </param>
  371.             ''' <param name="iMenu">
  372.             ''' The zero-based position at which to insert the first new menu item.
  373.             ''' </param>
  374.             ''' <param name="idCmdFirst">
  375.             ''' The minimum value that the handler can specify for a menu item identifier.
  376.             ''' </param>
  377.             ''' <param name="idCmdLast">
  378.             ''' The maximum value that the handler can specify for a menu item identifier.
  379.             ''' </param>
  380.             ''' <param name="uFlags">
  381.             ''' Optional flags that specify how the shortcut menu can be changed.
  382.             ''' The remaining bits of the low-order word are reserved by the system.
  383.             ''' The high-order word can be used for context-specific communications.
  384.             ''' The CMF_RESERVED value can be used to mask the low-order word.</param>
  385.             ''' <returns>System.Int32.</returns>
  386.             <PreserveSig>
  387.             Function QueryContextMenu(
  388.                  ByVal hmenu As IntPtr,
  389.                  ByVal iMenu As Integer,
  390.                  ByVal idCmdFirst As Integer,
  391.                  ByVal idCmdLast As Integer,
  392.                  ByVal uFlags As QueryContextMenuFlags
  393.         ) As Integer
  394.  
  395.             ''' <summary>
  396.             ''' Carries out the command associated with a shortcut menu item.
  397.             ''' </summary>
  398.             ''' <param name="pici">
  399.             ''' A pointer to a CMINVOKECOMMANDINFO or CMINVOKECOMMANDINFOEX structure that contains
  400.             ''' specifics about the command.</param>
  401.             ''' <returns>System.Int32.</returns>
  402.             <PreserveSig>
  403.             Function InvokeCommand(
  404.                  ByRef pici As CMINVOKECOMMANDINFO
  405.         ) As Integer
  406.  
  407.             ''' <summary>
  408.             ''' Gets information about a shortcut menu command,
  409.             ''' including the help string and the language-independent, or canonical, name for the command.
  410.             ''' </summary>
  411.             ''' <param name="idCmd">
  412.             ''' Menu command identifier offset.
  413.             ''' </param>
  414.             ''' <param name="uFlags">
  415.             ''' Flags specifying the information to return.
  416.             ''' </param>
  417.             ''' <param name="pwReserved">
  418.             ''' Reserved.
  419.             ''' Applications must specify NULL when calling this method and
  420.             ''' handlers must ignore this parameter when called.
  421.             ''' </param>
  422.             ''' <param name="pszName">
  423.             ''' The address of the buffer to receive the null-terminated string being retrieved.
  424.             ''' </param>
  425.             ''' <param name="cchMax">
  426.             ''' Size of the buffer, in characters, to receive the null-terminated string.
  427.             ''' </param>
  428.             ''' <returns>System.Int32.</returns>
  429.             <PreserveSig>
  430.             Function GetCommandString(
  431.                  ByVal idCmd As Integer,
  432.                  ByVal uFlags As GetCommandStringFlags,
  433.                  ByVal pwReserved As IntPtr,
  434.                  <MarshalAs(UnmanagedType.LPWStr)> ByVal pszName As StringBuilder,
  435.                  ByVal cchMax As Integer) As Integer
  436.         End Interface
  437.  
  438. #End Region
  439.  
  440. #Region " Enumerations "
  441.  
  442.         ''' <summary>
  443.         ''' Optional flags that specify how a shortcut menu can be changed.
  444.         ''' </summary>
  445.         <Description("'uFlags' parameter used in 'QueryContextMenu' function")>
  446.         Public Enum QueryContextMenuFlags As Integer
  447.  
  448.             REM NOTE: This Enum is only partially defined, don't use it in other contexts.
  449.  
  450.             ''' <summary>
  451.             ''' Indicates normal operation.
  452.             ''' A shortcut menu extension, namespace extension, or drag-and-drop handler can add all menu items.
  453.             ''' </summary>
  454.             CMF_NORMAL = &H0
  455.  
  456.         End Enum
  457.  
  458.         ''' <summary>
  459.         ''' Flags specifying the information to return.
  460.         ''' </summary>
  461.         <Description("'uFlags' parameter used in 'GetCommandString' function")>
  462.         Public Enum GetCommandStringFlags As Integer
  463.  
  464.             REM NOTE: This Enum is only partially defined, don't use it in other contexts.
  465.  
  466.             ''' <summary>
  467.             ''' Sets pszName to a Unicode string containing the language-independent command name for the menu item.
  468.             ''' </summary>
  469.             GCS_VERBW = &H4
  470.  
  471.         End Enum
  472.  
  473. #End Region
  474.  
  475. #Region " Structures "
  476.  
  477.         ''' <summary>
  478.         ''' Contains the accumulated size and item count information retrieved by the 'QueryRecycleBin' function.
  479.         ''' </summary>
  480.         <Description("'QueryRBInfo' parameter of 'QueryRecycleBin' function.")>
  481.         <StructLayout(LayoutKind.Sequential)> _
  482.         Public Structure SHQUERYRBINFO
  483.  
  484.             ''' <summary>
  485.             ''' The size of the structure, in bytes.
  486.             ''' This member must be filled in prior to calling the function.
  487.             ''' </summary>
  488.             Public StructSize As Integer
  489.  
  490.             ''' <summary>
  491.             ''' The total size of all the objects in the specified Recycle Bin, in bytes.
  492.             ''' </summary>
  493.             Public BinSize As Long
  494.  
  495.             ''' <summary>
  496.             ''' The total number of items in the specified Recycle Bin.
  497.             ''' </summary>
  498.             Public ItemCount As Long
  499.  
  500.         End Structure
  501.  
  502.         ''' <summary>
  503.         ''' Contains information needed by IContextMenu::InvokeCommand to invoke a shortcut menu command.
  504.         ''' </summary>
  505.         <Description("'pici' parameter used in 'InvokeCommand' function")>
  506.         <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
  507.         Structure CMINVOKECOMMANDINFO
  508.  
  509.             ''' <summary>
  510.             ''' The size of this structure, in bytes.
  511.             ''' </summary>
  512.             Public cbSize As Integer
  513.  
  514.             ''' <summary>
  515.             ''' Zero, or one or more of the CMINVOKECOMMANDINFO flags.
  516.             ''' </summary>
  517.             Public fMask As Integer
  518.  
  519.             ''' <summary>
  520.             ''' A handle to the window that is the owner of the shortcut menu.
  521.             ''' An extension can also use this handle as the owner of any message boxes or dialog boxes it displays.
  522.             ''' </summary>
  523.             Public hwnd As IntPtr
  524.  
  525.             ''' <summary>
  526.             ''' The address of a null-terminated string that specifies the language-independent name
  527.             ''' of the command to carry out.
  528.             ''' This member is typically a string when a command is being activated by an application.
  529.             ''' The system provides predefined constant values for the following command strings.
  530.             ''' </summary>
  531.             Public lpVerb As IntPtr
  532.  
  533.             ''' <summary>
  534.             ''' An optional string containing parameters that are passed to the command.
  535.             ''' The format of this string is determined by the command that is to be invoked.
  536.             ''' This member is always NULL for menu items inserted by a Shell extension.
  537.             ''' </summary>
  538.             Public lpParameters As String
  539.  
  540.             ''' <summary>
  541.             ''' An optional working directory name.
  542.             ''' This member is always NULL for menu items inserted by a Shell extension.
  543.             ''' </summary>
  544.             Public lpDirectory As String
  545.  
  546.             ''' <summary>
  547.             ''' A set of SW_ values to pass to the ShowWindow function if the command
  548.             ''' displays a window or starts an application.
  549.             ''' </summary>
  550.             Public nShow As Integer
  551.  
  552.             ''' <summary>
  553.             ''' An optional keyboard shortcut to assign to any application activated by the command.
  554.             ''' If the fMask parameter does not specify CMIC_MASK_HOTKEY, this member is ignored.
  555.             ''' </summary>
  556.             Public dwHotKey As Integer
  557.  
  558.             ''' <summary>
  559.             ''' An icon to use for any application activated by the command.
  560.             ''' If the fMask member does not specify CMIC_MASK_ICON, this member is ignored.
  561.             ''' </summary>
  562.             Public hIcon As IntPtr
  563.  
  564.         End Structure
  565.  
  566. #End Region
  567.  
  568.     End Class
  569.  
  570. #End Region
  571.  
  572. #Region " MainBin CLASS "
  573.  
  574.     ''' <summary>
  575.     ''' Retrieves information about the Main Recycle Bin, and perform other operations.
  576.     ''' The Main Recycle Bin is that one that also contains all the deleted items of the other recycle bins.
  577.     ''' </summary>
  578.     Public Class MainBin
  579.  
  580. #Region " Members "
  581.  
  582. #Region " Properties "
  583.  
  584.         ''' <summary>
  585.         ''' Gets all the items inside the Main recycle bin.
  586.         ''' </summary>
  587.         ''' <value>All the items.</value>
  588.         Public Shared ReadOnly Property Items As ShellObject()
  589.             Get
  590.                 Return Tools.GetDeletedItems(Nothing)
  591.             End Get
  592.         End Property
  593.  
  594.         ''' <summary>
  595.         ''' Gets all the files inside the Main recycle bin.
  596.         ''' </summary>
  597.         ''' <value>The files.</value>
  598.         Public Shared ReadOnly Property Files As ShellFile()
  599.             Get
  600.                 Return Tools.GetDeletedFiles(Nothing)
  601.             End Get
  602.         End Property
  603.  
  604.         ''' <summary>
  605.         ''' Gets all the folders inside the Main recycle bin.
  606.         ''' </summary>
  607.         ''' <value>The folders.</value>
  608.         Public Shared ReadOnly Property Folders As ShellFolder()
  609.             Get
  610.                 Return Tools.GetDeletedFolders(Nothing)
  611.             End Get
  612.         End Property
  613.  
  614.         ''' <summary>
  615.         ''' Gets the last deleted item inside the Main recycle bin.
  616.         ''' </summary>
  617.         ''' <value>The folders.</value>
  618.         Public Shared ReadOnly Property LastDeletedItem As ShellObject
  619.             Get
  620.                 Return Tools.GetLastDeletedItem(Nothing)
  621.             End Get
  622.         End Property
  623.  
  624.         ''' <summary>
  625.         ''' Gets the last deleted file inside the Main recycle bin.
  626.         ''' </summary>
  627.         ''' <value>The folders.</value>
  628.         Public Shared ReadOnly Property LastDeletedFile As ShellFile
  629.             Get
  630.                 Return Tools.GetLastDeletedFile(Nothing)
  631.             End Get
  632.         End Property
  633.  
  634.         ''' <summary>
  635.         ''' Gets the last deleted folder inside the Main recycle bin.
  636.         ''' </summary>
  637.         ''' <value>The folders.</value>
  638.         Public Shared ReadOnly Property LastDeletedFolder As ShellFolder
  639.             Get
  640.                 Return Tools.GetLastDeletedFolder(Nothing)
  641.             End Get
  642.         End Property
  643.  
  644.         ''' <summary>
  645.         ''' Gets the accumulated size of the Main recycle bin.
  646.         ''' </summary>
  647.         ''' <value>The folders.</value>
  648.         Public Shared ReadOnly Property Size As Long
  649.             Get
  650.                 Return Tools.GetSize(Nothing)
  651.             End Get
  652.         End Property
  653.  
  654.         ''' <summary>
  655.         ''' Gets the total deleted items count of the Main recycle bin.
  656.         ''' </summary>
  657.         ''' <value>The folders.</value>
  658.         Public Shared ReadOnly Property ItemsCount As Long
  659.             Get
  660.                 Return Tools.GetItemsCount(Nothing)
  661.             End Get
  662.         End Property
  663.  
  664. #End Region
  665.  
  666. #End Region
  667.  
  668. #Region " Public Methods "
  669.  
  670.         ''' <summary>
  671.         ''' Empties the Master Recycle Bin.
  672.         ''' </summary>
  673.         ''' <param name="Flags">Indicates the recycle bin behavior.</param>
  674.         ''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns>
  675.         Public Shared Function Empty(Optional ByVal Flags As Tools.RecycleBinFlags =
  676.                                                              Tools.RecycleBinFlags.DontShowConfirmation) As Boolean
  677.  
  678.             Return NativeMethods.EmptyRecycleBin(IntPtr.Zero, Nothing, Flags)
  679.  
  680.         End Function
  681.  
  682.         ''' <summary>
  683.         ''' Refreshes the Recycle bin icon.
  684.         ''' </summary>
  685.         ''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns>
  686.         Public Shared Function RefreshIcon() As Boolean
  687.  
  688.             Return NativeMethods.UpdateRecycleBinIcon()
  689.  
  690.         End Function
  691.  
  692. #End Region
  693.  
  694. #Region " Hidden methods "
  695.  
  696.         ' These methods are purposely hidden from Intellisense just to look better without unneeded methods.
  697.         ' NOTE: The methods can be re-enabled at any-time if needed.
  698.  
  699.         ''' <summary>
  700.         ''' Equalses this instance.
  701.         ''' </summary>
  702.         <EditorBrowsable(EditorBrowsableState.Never)>
  703.         Public Shadows Sub Equals()
  704.         End Sub
  705.  
  706.         ''' <summary>
  707.         ''' References the equals.
  708.         ''' </summary>
  709.         <EditorBrowsable(EditorBrowsableState.Never)>
  710.         Public Shadows Sub ReferenceEquals()
  711.         End Sub
  712.  
  713. #End Region
  714.  
  715.     End Class
  716.  
  717. #End Region
  718.  
  719. #Region " Tools CLASS "
  720.  
  721.     ''' <summary>
  722.     ''' Retrieves information about the Recycle Bin of an specific drive,
  723.     ''' and perform other operations.
  724.     ''' </summary>
  725.     Public Class Tools
  726.  
  727. #Region " Members "
  728.  
  729. #Region " Enumerations "
  730.  
  731.         ''' <summary>
  732.         ''' Specifies the Recycle bin behavior.
  733.         ''' </summary>
  734.         <Description("'RecycleBinFlags' parameter used in 'EmptyRecycleBin' function.")>
  735.         <Flags>
  736.         Public Enum RecycleBinFlags As Integer
  737.  
  738.             ''' <summary>
  739.             ''' The dont show confirmation
  740.             ''' </summary>
  741.             DontShowConfirmation = &H1
  742.  
  743.             ''' <summary>
  744.             ''' The dont show progress UI
  745.             ''' </summary>
  746.             DontShowProgressUI = &H2
  747.  
  748.             ''' <summary>
  749.             ''' The dont play sound
  750.             ''' </summary>
  751.             DontPlaySound = &H4
  752.  
  753.         End Enum
  754.  
  755.         ''' <summary>
  756.         ''' A verb is a string used to specify a particular action that an item supports.
  757.         ''' Invoking a verb is equivalent to selecting a command from an item's context menu.
  758.         ''' Typically, invoking a verb launches a related application.
  759.         ''' For example, invoking the "open" verb on a ".txt" file opens the file with a text editor, usually Notepad.
  760.         ''' </summary>
  761.         <Description("'Verb' parameter used in 'InvokeItemVerb' method.")>
  762.         Public Enum ItemVerbs As Integer
  763.             Open = 0
  764.             Delete = 1
  765.             Undelete = 2
  766.             Properties = 3
  767.         End Enum
  768.  
  769. #End Region
  770.  
  771. #End Region
  772.  
  773. #Region " Public Methods "
  774.  
  775.         ''' <summary>
  776.         ''' Empties the Recycle Bin of an specific Drive.
  777.         ''' </summary>
  778.         ''' <param name="DriveLetter">
  779.         ''' Indicates an specific recycle bin drive letter.
  780.         ''' If this parameter is 'Nothing' then it empties all the Recycle Bins.
  781.         ''' </param>
  782.         ''' <param name="Flags">Indicates the recycle bin behavior.</param>
  783.         ''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns>
  784.         Public Shared Function Empty(Optional ByVal DriveLetter As Char = Nothing,
  785.                                      Optional ByVal Flags As RecycleBinFlags =
  786.                                                              RecycleBinFlags.DontShowConfirmation) As Boolean
  787.  
  788.             Return NativeMethods.EmptyRecycleBin(IntPtr.Zero, DriveLetter, Flags)
  789.  
  790.         End Function
  791.  
  792.         ''' <summary>
  793.         ''' Gets the recycle bin accumulated size (in bytes) of an specific Drive.
  794.         ''' </summary>
  795.         ''' <param name="DriveLetter">Indicates an specific recycle bin drive letter.</param>
  796.         ''' <returns>System.Int64.</returns>
  797.         Public Shared Function GetSize(ByVal DriveLetter As Char) As Long
  798.  
  799.             Dim sqrbi As New NativeMethods.SHQUERYRBINFO() With
  800.                 {
  801.                     .StructSize = Marshal.SizeOf(GetType(NativeMethods.SHQUERYRBINFO))
  802.                 }
  803.  
  804.             Select Case DriveLetter = Nothing
  805.  
  806.                 Case True
  807.                     NativeMethods.QueryRecycleBin(String.Empty, sqrbi)
  808.  
  809.                 Case Else
  810.                     NativeMethods.QueryRecycleBin(String.Format("{0}:\", DriveLetter), sqrbi)
  811.  
  812.             End Select
  813.  
  814.             Return sqrbi.BinSize
  815.  
  816.         End Function
  817.  
  818.         ''' <summary>
  819.         ''' Gets the total item count inside the recycle bin of an specific Drive.
  820.         ''' </summary>
  821.         ''' <param name="DriveLetter">Indicates an specific recycle bin drive letter.</param>
  822.         ''' <returns>System.Int64.</returns>
  823.         Public Shared Function GetItemsCount(ByVal DriveLetter As Char) As Long
  824.  
  825.             Dim sqrbi As New NativeMethods.SHQUERYRBINFO() With
  826.                 {
  827.                     .StructSize = Marshal.SizeOf(GetType(NativeMethods.SHQUERYRBINFO))
  828.                 }
  829.  
  830.             Select Case DriveLetter = Nothing
  831.  
  832.                 Case True
  833.                     NativeMethods.QueryRecycleBin(String.Empty, sqrbi)
  834.  
  835.                 Case Else
  836.                     NativeMethods.QueryRecycleBin(String.Format("{0}:\", DriveLetter), sqrbi)
  837.  
  838.             End Select
  839.  
  840.             Return sqrbi.ItemCount
  841.  
  842.         End Function
  843.  
  844.         ''' <summary>
  845.         ''' Gets all the deleted items inside the recycle bin of an specific Drive,
  846.         ''' this means all Files and Folders.
  847.         ''' </summary>
  848.         ''' <param name="DriveLetter">Indicates an specific recycle bin drive letter.</param>
  849.         ''' <returns>ShellObject[][].</returns>
  850.         Public Shared Function GetDeletedItems(ByVal DriveLetter As Char) As ShellObject()
  851.  
  852.             Return (From Item As ShellObject In RecycleBin
  853.                     Where If(DriveLetter = Nothing,
  854.                              Item.Name = Item.Name,
  855.                              Item.Name.StartsWith(DriveLetter,
  856.                                                   StringComparison.InvariantCultureIgnoreCase))).
  857.                     ToArray()
  858.  
  859.         End Function
  860.  
  861.         ''' <summary>
  862.         ''' Gets the deleted files inside the recycle bin of an specific Drive.
  863.         ''' </summary>
  864.         ''' <param name="DriveLetter">Indicates an specific recycle bin drive letter.</param>
  865.         ''' <returns>ShellFile[][].</returns>
  866.         Public Shared Function GetDeletedFiles(ByVal DriveLetter As Char) As ShellFile()
  867.  
  868.             Return GetDeletedItems(Of ShellFile)(DriveLetter)
  869.  
  870.         End Function
  871.  
  872.         ''' <summary>
  873.         ''' Gets the deleted folders inside the recycle bin of an specific Drive.
  874.         ''' </summary>
  875.         ''' <param name="DriveLetter">Indicates an specific recycle bin drive letter.</param>
  876.         ''' <returns>IKnownFolder[][].</returns>
  877.         Public Shared Function GetDeletedFolders(ByVal DriveLetter As Char) As ShellFolder()
  878.  
  879.             Return GetDeletedItems(Of FileSystemKnownFolder)(DriveLetter).
  880.                    Cast(Of ShellFolder).
  881.                    Concat(GetDeletedItems(Of NonFileSystemKnownFolder)(DriveLetter)).
  882.                    Cast(Of ShellFolder).ToArray
  883.  
  884.         End Function
  885.  
  886.         ''' <summary>
  887.         ''' Gets the last deleted item inside the recycle bin of an specific Drive.
  888.         ''' </summary>
  889.         ''' <param name="DriveLetter">Indicates an specific recycle bin drive letter.</param>
  890.         ''' <returns>ShellObject.</returns>
  891.         Public Shared Function GetLastDeletedItem(ByVal DriveLetter As Char) As ShellObject
  892.  
  893.             Return (From Item As ShellObject
  894.                     In GetDeletedItems(DriveLetter)
  895.                     Order By Item.Properties.GetProperty("DateDeleted").ValueAsObject
  896.                     Descending).
  897.                     First
  898.  
  899.         End Function
  900.  
  901.         ''' <summary>
  902.         ''' Gets the last deleted file inside the recycle bin of an specific Drive.
  903.         ''' </summary>
  904.         ''' <param name="DriveLetter">Indicates an specific recycle bin drive letter.</param>
  905.         ''' <returns>ShellFile.</returns>
  906.         Public Shared Function GetLastDeletedFile(ByVal DriveLetter As Char) As ShellFile
  907.  
  908.             Return (From Item As ShellFile
  909.                     In GetDeletedItems(Of ShellFile)(DriveLetter)
  910.                     Order By Item.Properties.GetProperty("DateDeleted").ValueAsObject
  911.                     Descending).
  912.                     First
  913.  
  914.         End Function
  915.  
  916.         ''' <summary>
  917.         ''' Gets the last deleted folder inside the recycle bin of an specific Drive.
  918.         ''' </summary>
  919.         ''' <param name="DriveLetter">Indicates an specific recycle bin drive letter.</param>
  920.         ''' <returns>ShellFolder.</returns>
  921.         Public Shared Function GetLastDeletedFolder(ByVal DriveLetter As Char) As ShellFolder
  922.  
  923.             Return (From Item As ShellFolder
  924.                     In GetDeletedFolders(DriveLetter)
  925.                     Order By Item.Properties.GetProperty("DateDeleted").ValueAsObject
  926.                     Descending).
  927.                     First
  928.  
  929.         End Function
  930.  
  931.         ''' <summary>
  932.         ''' Permanently deletes a deleted Item.
  933.         ''' </summary>
  934.         ''' <param name="Item">The item to permanently delete.</param>
  935.         Public Shared Sub DeleteItem(ByVal Item As ShellObject)
  936.  
  937.             InvokeItemVerb(Item, ItemVerbs.Delete)
  938.  
  939.         End Sub
  940.  
  941.         ''' <summary>
  942.         ''' Undeletes a deleted Item.
  943.         ''' </summary>
  944.         ''' <param name="Item">The item to undelete.</param>
  945.         Public Shared Sub UndeleteItem(ByVal Item As ShellObject)
  946.  
  947.             InvokeItemVerb(Item, ItemVerbs.Undelete)
  948.  
  949.         End Sub
  950.  
  951.         ''' <summary>
  952.         ''' Invokes a verb on a ShellObject item.
  953.         ''' </summary>
  954.         ''' <param name="ShellObject">
  955.         ''' Indicates the item.
  956.         ''' </param>
  957.         ''' <param name="Verb">
  958.         ''' Indicates the verb to invoke on the item.
  959.         ''' </param>
  960.         ''' <exception cref="System.ArgumentNullException">ParsingPath</exception>
  961.         ''' <exception cref="System.ComponentModel.Win32Exception">
  962.         ''' </exception>
  963.         Public Shared Sub InvokeItemVerb(ByVal [ShellObject] As ShellObject,
  964.                                           ByVal Verb As String)
  965.  
  966.             Dim [ShellItem] As NativeMethods.IShellItem = Nothing
  967.             Dim menu As NativeMethods.IContextMenu = Nothing
  968.             Dim BHID_SFUIObject As New Guid("{3981e225-f559-11d3-8e3a-00c04f6837d5}")
  969.             Dim ci As New NativeMethods.CMINVOKECOMMANDINFO
  970.             Dim cm As New ContextMenu
  971.             Dim sb As New StringBuilder(256)
  972.             Dim hr As Integer = -1
  973.             Dim VerbId As Integer = -1
  974.             Dim MenuItemId As Integer = -1
  975.  
  976.             ' Get an item from the item parsing-path
  977.             hr = NativeMethods.SHCreateItemFromParsingName([ShellObject].Properties.System.ParsingPath.Value,
  978.                                                            IntPtr.Zero,
  979.                                                            GetType(NativeMethods.IShellItem).GUID,
  980.                                                            [ShellItem])
  981.  
  982.             If hr < 0 Then
  983.                 Throw New Win32Exception(hr)
  984.             End If
  985.  
  986.             ' Get the context menu from the item
  987.             hr = [ShellItem].BindToHandler(IntPtr.Zero, BHID_SFUIObject,
  988.                                            GetType(NativeMethods.IContextMenu).GUID,
  989.                                            menu)
  990.  
  991.             If hr < 0 Then
  992.                 Throw New Win32Exception(hr)
  993.             End If
  994.  
  995.             ' Build a fake context menu so we can scan it for the verb's menu id.
  996.             hr = menu.QueryContextMenu(cm.Handle, 0, 0, -1,
  997.                                        NativeMethods.QueryContextMenuFlags.CMF_NORMAL)
  998.  
  999.             If hr < 0 Then
  1000.                 Throw New Win32Exception(hr)
  1001.             End If
  1002.  
  1003.             For i As Integer = 0 To (NativeMethods.GetMenuItemCount(cm.Handle) - 1)
  1004.  
  1005.                 MenuItemId = NativeMethods.GetMenuItemID(cm.Handle, i)
  1006.  
  1007.                 If MenuItemId < 0 Then
  1008.                     Continue For
  1009.                 End If
  1010.  
  1011.                 hr = menu.GetCommandString(MenuItemId, NativeMethods.GetCommandStringFlags.GCS_VERBW,
  1012.                                            IntPtr.Zero, sb, sb.Capacity)
  1013.  
  1014.                 If sb.ToString().Equals(Verb, StringComparison.InvariantCultureIgnoreCase) Then
  1015.                     VerbId = MenuItemId
  1016.                     Exit For
  1017.                 End If
  1018.  
  1019.             Next i
  1020.  
  1021.             If VerbId < 0 Then
  1022.                 Throw New Win32Exception(String.Format("Verb '{0}' is not supported by the item.", Verb))
  1023.             End If
  1024.  
  1025.             ' Invoke the Verb.
  1026.             With ci
  1027.                 .cbSize = Marshal.SizeOf(GetType(NativeMethods.CMINVOKECOMMANDINFO))
  1028.                 .lpVerb = New IntPtr(VerbId)
  1029.             End With
  1030.  
  1031.             hr = menu.InvokeCommand(ci)
  1032.  
  1033.             If hr < 0 Then
  1034.                 Throw New Win32Exception(hr)
  1035.             End If
  1036.  
  1037.         End Sub
  1038.  
  1039. #End Region
  1040.  
  1041. #Region " Private Methods "
  1042.  
  1043.         ''' <summary>
  1044.         ''' Gets the deleted items inside the recycle bin.
  1045.         ''' </summary>
  1046.         ''' <typeparam name="T">Indicates the kind of deleted items to retrieve (Files, Folders, etc...)</typeparam>
  1047.         ''' <param name="DriveLetter">Indicates an specific recycle bin drive letter.
  1048.         ''' If this parameter is 'Nothing' then it returns the merged contents of all the Recycle Bins.</param>
  1049.         ''' <returns>``0[][].</returns>
  1050.         Private Shared Function GetDeletedItems(Of T)(Optional ByVal DriveLetter As Char = Nothing) As T()
  1051.  
  1052.             Return (From Item As ShellObject In RecycleBin
  1053.                     Where If(DriveLetter = Nothing,
  1054.                              Item.GetType = GetType(T),
  1055.                              Item.GetType = GetType(T) _
  1056.                                   AndAlso Item.Name.StartsWith(DriveLetter,
  1057.                                                                StringComparison.InvariantCultureIgnoreCase))).
  1058.                     Cast(Of T).
  1059.                     ToArray()
  1060.  
  1061.         End Function
  1062.  
  1063.         ''' <summary>
  1064.         ''' Invokes a verb on a ShellObject item.
  1065.         ''' </summary>
  1066.         ''' <param name="ShellObject">
  1067.         ''' Indicates the item.
  1068.         ''' </param>
  1069.         ''' <param name="Verb">
  1070.         ''' Indicates the verb to invoke on the item.
  1071.         ''' </param>
  1072.         Private Shared Sub InvokeItemVerb(ByVal [ShellObject] As ShellObject,
  1073.                                           ByVal Verb As ItemVerbs)
  1074.  
  1075.             InvokeItemVerb([ShellObject], Verb.ToString)
  1076.  
  1077.         End Sub
  1078.  
  1079. #End Region
  1080.  
  1081. #Region " Hidden methods "
  1082.  
  1083.         ' These methods are purposely hidden from Intellisense just to look better without unneeded methods.
  1084.         ' NOTE: The methods can be re-enabled at any-time if needed.
  1085.  
  1086.         ''' <summary>
  1087.         ''' Equalses this instance.
  1088.         ''' </summary>
  1089.         <EditorBrowsable(EditorBrowsableState.Never)>
  1090.         Public Shadows Sub Equals()
  1091.         End Sub
  1092.  
  1093.         ''' <summary>
  1094.         ''' References the equals.
  1095.         ''' </summary>
  1096.         <EditorBrowsable(EditorBrowsableState.Never)>
  1097.         Public Shadows Sub ReferenceEquals()
  1098.         End Sub
  1099.  
  1100. #End Region
  1101.  
  1102.     End Class
  1103.  
  1104. #End Region
  1105.  
  1106. #Region " Hidden methods "
  1107.  
  1108.     ' These methods are purposely hidden from Intellisense just to look better without unneeded methods.
  1109.     ' NOTE: The methods can be re-enabled at any-time if needed.
  1110.  
  1111.     ''' <summary>
  1112.     ''' Equalses this instance.
  1113.     ''' </summary>
  1114.     <EditorBrowsable(EditorBrowsableState.Never)>
  1115.     Public Shadows Sub Equals()
  1116.     End Sub
  1117.  
  1118.     ''' <summary>
  1119.     ''' References the equals.
  1120.     ''' </summary>
  1121.     <EditorBrowsable(EditorBrowsableState.Never)>
  1122.     Public Shadows Sub ReferenceEquals()
  1123.     End Sub
  1124.  
  1125. #End Region
  1126.  
  1127. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement