How to retrieve the 'Deletion Date' property of an Item stored in the Recycle Bin using Windows API Code Pack?

1.4k views Asked by At

UPDATE 2

Finally testing most of the WindowsAPICodePack interfaces I've found bymyself the way to acces to the deleted Files on the RecycleBin.

The unique problem now is that I need to know how to acces the required property bag to retrieve the deletion date of each file (and folder and link), This is a sample code:

Dim RecycleBin As IKnownFolder = KnownFolders.RecycleBin

For Each File As ShellFile In (From Item As ShellObject In RecycleBin
                               Where Item.GetType = GetType(ShellFile))

    MsgBox(File.Name)
    MsgBox(File.Properties.System.IsDeleted.Value) ' It's empty.
    MsgBox(File.Properties.System.DateAcquired.Value) ' This is not the correct value.
    MsgBox(File.Properties.System.DateArchived.Value) ' This is also not the correct value.
    MsgBox(File.Properties.System.DateCompleted.Value) ' This is also not the correct value.
    MsgBox(File.Properties.System.DateCreated.Value) ' This is also not the correct value.
    MsgBox(File.Properties.System.DateImported.Value) ' This is also not the correct value.

Next File
2

There are 2 answers

6
Nathan M On BEST ANSWER

The Windows API Code Pack might be of some help to you. It has more extensive (full) implementations of most shell interfaces. You will have to open up the code pack items (InternalsVisibleTo application manifest attribute, or just change all the internal interfaces to external) in order to use them away from the given wrappers.

As for the delete date: That's contained in the property bag of the shell item.
The great Raymond Chen, who has been a developer at Microsoft since the beginning of time and personally gave birth to the Windows Shell wrote a complete walk-through article about how to go about doing it in C++, aptly named 'How can I get information about the items in the Recycle Bin?'

You can, with a little bit of logical deduction, take the bits you need away from it and produce your own managed implementation.

Between those two links, you now have all the knowledge in your possession to solve your problem and then some.

2
ElektroStudios On

To retrieve the DateDeleted property of an Item is simple as this:

Private Sub Test() Handles MyBase.Shown

    ' Get all the desired deleted items inside the Recycle Bin using WindowsAPICodePack.
    ' ( In my case I only retrieve the deleted files excluding folders. )
    Dim RecycledFiles As ShellFile() = RecycleBin.MasterBin.Files

    ' Loop through the deleted Items.
    For Each Item As ShellFile In RecycledFiles

        ' Append the full name
        sb.AppendLine(Item.Name)

        ' Append the DateDeleted.
        sb.AppendLine(Item.Properties.GetProperty("DateDeleted").ValueAsObject.ToString)

        MsgBox(sb.ToString)
        sb.Clear()

    Next Item

End Sub

Then to retrieve the last deleted file forexample:

''' <summary>
''' Gets the last deleted file inside recycle bin.
''' </summary>
''' <returns>ShellFile.</returns>
Public Shared Function GetLastDeletedFile() As ShellFile

    Return (From Item As ShellFile In GetDeletedItems(Of ShellFile)(Nothing)
            Order By Item.Properties.GetProperty("DateDeleted").ValueAsObject).Last

End Function

And with this snippet we can retrieve the other property names and values of each:

Dim sb As New System.Text.StringBuilder

Private Sub Test() Handles MyBase.Shown

    ' Get all the desired deleted items inside the Recycle Bin using WindowsAPICodePack.
    ' ( In my case I only retrieve the deleted files excluding folders. )
    Dim RecycledFiles As ShellFile() = RecycleBin.MasterBin.Files

    ' Loop through the deleted Items.
    For Each Item As ShellFile In RecycledFiles

        ' Append the full name (It's String type)
        sb.AppendLine(Item.Name)

        ' Loop through the Item properties.
        For Each prop In Item.Properties.DefaultPropertyCollection

            ' Append an empty line
            sb.AppendLine()

            ' Append the property name (It's String type)
            sb.Append(prop.CanonicalName)

            ' Append the property value (It's a generic Object type)
            If prop.ValueAsObject IsNot Nothing Then
                sb.Append(" = " & prop.ValueAsObject.ToString)
            Else
                sb.Append(" = NULL")
            End If

            MsgBox(sb.ToString)

        Next prop

    Next Item

End Sub

Just another example:

Private Sub Test() Handles MyBase.Shown

    ' Get all the deleted items inside the Recycle Bin using WindowsAPICodePack.
    Dim RecycledItems As ShellObject() = RecycleBin.MainBin.Items

    ' Loop through the deleted Items (Ordered by Deletion Date).
    For Each Item As ShellFile In (From itm In RecycledItems
                                   Order By itm.Properties.GetProperty("DateDeleted").ValueAsObject Ascending)

        ' Append the property bags information.
        sb.AppendLine(String.Format("Full Name: {0}",
                                    Item.Name))

        sb.AppendLine(String.Format("Item Name: {0}",
                                    Item.Properties.GetProperty("System.ItemNameDisplay").ValueAsObject))

        sb.AppendLine(String.Format("Deleted From: {0}",
                                    Item.Properties.GetProperty("DeletedFrom").ValueAsObject))

        sb.AppendLine(String.Format("Item Type: {0}",
                                    Item.Properties.GetProperty("System.ItemTypeText").ValueAsObject))

        sb.AppendLine(String.Format("Item Size: {0}",
                                    Item.Properties.GetProperty("System.Size").ValueAsObject))

        sb.AppendLine(String.Format("Attributes: {0}",
                                    [Enum].Parse(GetType(IO.FileAttributes),
                                                 Item.Properties.GetProperty("System.FileAttributes").ValueAsObject.ToString)))

        sb.AppendLine(String.Format("Date Deleted: {0}",
                                    Item.Properties.GetProperty("DateDeleted").ValueAsObject))

        sb.AppendLine(String.Format("Date Modified: {0}",
                                    Item.Properties.GetProperty("System.DateModified").ValueAsObject))

        sb.AppendLine(String.Format("Date Created: {0}",
                                    Item.Properties.GetProperty("System.DateCreated").ValueAsObject))

        MsgBox(sb.ToString)
        sb.Clear()

    Next Item

End Sub