few days ago I've write this approach to temporally resolve the problem, but I'm not 100% secure that the method will work properly in all the scenarios (for example the user can press ctrl+z to restore a file deletion and my method logic uses the datetime property of the files to try to pick the last deleted which I'm not 100% secure), this works by now, but I would like to learn how to API hook instead of doing this.
Also obviously this will not work with a permanent file deletion.
Imports System.IO
Imports Shell32
Public Class Test
Private SH As New Shell
Private RecycleBin As Folder = SH.NameSpace(ShellSpecialFolderConstants.ssfBITBUCKET)
Private WithEvents FSW As New FileSystemWatcher
Private Shadows Sub Load() _
Handles MyBase.Load
With FSW
.Path = "C:\Test"
.IncludeSubdirectories = True
.Filter = "*"
.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.DirectoryName
.EnableRaisingEvents = True
End With
End Sub
Private Sub OnItemDeleted(sender As FileSystemWatcher, e As FileSystemEventArgs) _
Handles FSW.Deleted
Dim DeletedItems As IEnumerable(Of FolderItem) =
RecycleBin.Items.Cast(Of FolderItem).
Where(Function(Item) Item.Name = e.Name).
OrderBy(Function(Item) Item.ModifyDate)
Dim LastDeletedItem As Shell32.FolderItem = DeletedItems.LastOrDefault
If LastDeletedItem IsNot Nothing Then
If (LastDeletedItem.IsFolder AndAlso Directory.Exists(e.FullPath)) _
OrElse (Not LastDeletedItem.IsFolder AndAlso File.Exists(e.FullPath)) Then
Throw New Exception(String.Format("¿ Item has been restored ?: {0}", e.FullPath))
Exit Sub
End If
LastDeletedItem.InvokeVerb("undelete")
End If
End Sub
End Class