@@ -82,47 +82,64 @@ public bool DeleteAllAsync()
82
82
/// <inheritdoc/>
83
83
public unsafe bool RestoreAllAsync ( )
84
84
{
85
- // Get IShellItem for Recycle Bin
86
- var recycleBinFolderId = FOLDERID . FOLDERID_RecycleBinFolder ;
87
- var shellItemGuid = typeof ( IShellItem ) . GUID ;
88
85
IShellItem * recycleBinFolderShellItem = default ;
89
- PInvoke . SHGetKnownFolderItem ( & recycleBinFolderId , KNOWN_FOLDER_FLAG . KF_FLAG_DEFAULT , HANDLE . Null , & shellItemGuid , ( void * * ) & recycleBinFolderShellItem ) ;
90
-
91
- // Get IEnumShellItems for Recycle Bin
92
- Guid enumShellItemGuid = typeof ( IEnumShellItems ) . GUID ;
93
86
IEnumShellItems * enumShellItems = default ;
94
- var enumItemsBHID = BHID . BHID_EnumItems ;
95
- recycleBinFolderShellItem ->BindToHandler ( null , & enumItemsBHID , & enumShellItemGuid , ( void * * ) & enumShellItems ) ;
96
-
97
- // Initialize how to perform the operation
98
- PInvoke . CoCreateInstance ( typeof ( FileOperation ) . GUID , null , CLSCTX . CLSCTX_LOCAL_SERVER , out IFileOperation * fileOperation ) ;
99
- fileOperation ->SetOperationFlags ( FILEOPERATION_FLAGS . FOF_NO_UI ) ;
100
- fileOperation ->SetOwnerWindow ( new ( MainWindow . Instance . WindowHandle ) ) ;
101
-
87
+ IFileOperation * pFileOperation = default ;
102
88
IShellItem * pShellItem = default ;
103
- while ( enumShellItems ->Next ( 1 , & pShellItem ) == HRESULT . S_OK )
89
+
90
+ try
104
91
{
105
- // Get original path
106
- pShellItem ->QueryInterface ( typeof ( IShellItem2 ) . GUID , out var pShellItem2Ptr ) ;
107
- var pShellItem2 = ( IShellItem2 * ) pShellItem2Ptr ;
108
- PInvoke . PSGetPropertyKeyFromName ( "System.Recycle.DeletedFrom" , out var originalPathPropertyKey ) ;
109
- pShellItem2 ->GetString ( originalPathPropertyKey , out var szOriginalPath ) ;
110
-
111
- // Get IShellItem of the original path
112
- PInvoke . SHCreateItemFromParsingName ( szOriginalPath . ToString ( ) , null , typeof ( IShellItem ) . GUID , out var pOriginalPathShellItemPtr ) ;
113
- var pOriginalPathShellItem = ( IShellItem * ) pOriginalPathShellItemPtr ;
114
-
115
- // Define to move the shell item
116
- fileOperation ->MoveItem ( pShellItem , pOriginalPathShellItem , new PCWSTR ( ) , null ) ;
92
+ // Get IShellItem for Recycle Bin
93
+ var recycleBinFolderId = FOLDERID . FOLDERID_RecycleBinFolder ;
94
+ var shellItemGuid = typeof ( IShellItem ) . GUID ;
95
+ PInvoke . SHGetKnownFolderItem ( & recycleBinFolderId , KNOWN_FOLDER_FLAG . KF_FLAG_DEFAULT , HANDLE . Null , & shellItemGuid , ( void * * ) & recycleBinFolderShellItem ) ;
96
+
97
+ // Get IEnumShellItems for Recycle Bin
98
+ Guid enumShellItemGuid = typeof ( IEnumShellItems ) . GUID ;
99
+ var enumItemsBHID = BHID . BHID_EnumItems ;
100
+ recycleBinFolderShellItem ->BindToHandler ( null , & enumItemsBHID , & enumShellItemGuid , ( void * * ) & enumShellItems ) ;
101
+
102
+ // Initialize how to perform the operation
103
+ PInvoke . CoCreateInstance ( typeof ( FileOperation ) . GUID , null , CLSCTX . CLSCTX_LOCAL_SERVER , out pFileOperation ) ;
104
+ pFileOperation ->SetOperationFlags ( FILEOPERATION_FLAGS . FOF_NO_UI ) ;
105
+ pFileOperation ->SetOwnerWindow ( new ( MainWindow . Instance . WindowHandle ) ) ;
106
+
107
+ while ( enumShellItems ->Next ( 1 , & pShellItem ) == HRESULT . S_OK )
108
+ {
109
+ // Get original path
110
+ pShellItem ->QueryInterface ( typeof ( IShellItem2 ) . GUID , out var pShellItem2Ptr ) ;
111
+ var pShellItem2 = ( IShellItem2 * ) pShellItem2Ptr ;
112
+ PInvoke . PSGetPropertyKeyFromName ( "System.Recycle.DeletedFrom" , out var originalPathPropertyKey ) ;
113
+ pShellItem2 ->GetString ( originalPathPropertyKey , out var szOriginalPath ) ;
114
+ pShellItem2 ->Release ( ) ;
115
+
116
+ // Get IShellItem of the original path
117
+ PInvoke . SHCreateItemFromParsingName ( szOriginalPath . ToString ( ) , null , typeof ( IShellItem ) . GUID , out var pOriginalPathShellItemPtr ) ;
118
+ var pOriginalPathShellItem = ( IShellItem * ) pOriginalPathShellItemPtr ;
119
+
120
+ // Define to move the shell item
121
+ pFileOperation ->MoveItem ( pShellItem , pOriginalPathShellItem , new PCWSTR ( ) , null ) ;
122
+ }
123
+
124
+ // Perform
125
+ pFileOperation ->PerformOperations ( ) ;
126
+
127
+ // Reset the icon
128
+ Win32PInvoke . SHUpdateRecycleBinIcon ( ) ;
129
+
130
+ return true ;
131
+ }
132
+ catch
133
+ {
134
+ return false ;
135
+ }
136
+ finally
137
+ {
138
+ recycleBinFolderShellItem ->Release ( ) ;
139
+ enumShellItems ->Release ( ) ;
140
+ pFileOperation ->Release ( ) ;
141
+ pShellItem ->Release ( ) ;
117
142
}
118
-
119
- // Perform
120
- fileOperation ->PerformOperations ( ) ;
121
-
122
- // Reset the icon
123
- Win32PInvoke . SHUpdateRecycleBinIcon ( ) ;
124
-
125
- return true ;
126
143
}
127
144
}
128
145
}
0 commit comments