5
5
using System . Collections . Specialized ;
6
6
using System . Runtime . InteropServices ;
7
7
using System . Text ;
8
- using Vanara . Windows . Shell ;
9
8
using Windows . Win32 ;
10
9
using Windows . Win32 . Foundation ;
11
10
using Windows . Win32 . System . Com ;
11
+ using Windows . Win32 . System . SystemServices ;
12
12
using Windows . Win32 . UI . Shell ;
13
13
using Windows . Win32 . UI . WindowsAndMessaging ;
14
14
@@ -115,7 +115,6 @@ public unsafe bool Remove(RecentItem item)
115
115
{
116
116
try
117
117
{
118
- /*
119
118
var bhid = PInvoke . BHID_SFUIObject ;
120
119
var contextMenuIid = typeof ( IContextMenu ) . GUID ;
121
120
using ComPtr < IContextMenu > pContextMenu = default ;
@@ -127,32 +126,16 @@ public unsafe bool Remove(RecentItem item)
127
126
CMINVOKECOMMANDINFO cmi = default ;
128
127
cmi . cbSize = ( uint ) sizeof ( CMINVOKECOMMANDINFO ) ;
129
128
cmi . nShow = ( int ) SHOW_WINDOW_CMD . SW_HIDE ;
130
- fixed (byte* pVerb = Encoding.ASCII.GetBytes("remove"))
131
- cmi.lpVerb = new(pVerb);
132
129
133
- // Invoke the verb
130
+ // Try unpin first for pinned files
131
+ fixed ( byte * pVerb = Encoding . ASCII . GetBytes ( "unpinfromhome" ) )
132
+ cmi . lpVerb = new ( pVerb ) ;
134
133
hr = pContextMenu . Get ( ) ->InvokeCommand ( cmi ) ;
135
- if (hr.Value < 0)
136
- throw new COMException("Failed to remove the recent item from Recent.", hr.Value);
137
- */
138
-
139
- using ComPtr < IApplicationDestinations > pApplicationDestinations = default ;
140
- var applicationDestinationsIid = typeof ( IApplicationDestinations ) . GUID ;
141
- var applicationDestinationsInstanceIid = typeof ( ApplicationDestinations ) . GUID ;
142
134
143
- PInvoke . CoCreateInstance (
144
- & applicationDestinationsInstanceIid ,
145
- null ,
146
- CLSCTX . CLSCTX_LOCAL_SERVER ,
147
- & applicationDestinationsIid ,
148
- ( void * * ) pApplicationDestinations . GetAddressOf ( ) )
149
- . ThrowOnFailure ( ) ;
150
-
151
- // Set App ID
152
- fixed ( char * pszAppId = "Microsoft.Windows.Explorer" )
153
- pApplicationDestinations . Get ( ) ->SetAppID ( pszAppId ) ;
154
-
155
- pApplicationDestinations . Get ( ) ->RemoveDestination ( ( IUnknown * ) item . ShellItem . Get ( ) ) ;
135
+ // Remove recent files
136
+ fixed ( byte * pVerb = Encoding . ASCII . GetBytes ( "remove" ) )
137
+ cmi . lpVerb = new ( pVerb ) ;
138
+ hr = pContextMenu . Get ( ) ->InvokeCommand ( cmi ) ;
156
139
157
140
return true ;
158
141
}
@@ -168,25 +151,7 @@ public unsafe bool Clear()
168
151
{
169
152
try
170
153
{
171
- //PInvoke.SHAddToRecentDocs((uint)SHARD.SHARD_PIDL, null);
172
-
173
- using ComPtr < IApplicationDestinations > pApplicationDestinations = default ;
174
- var applicationDestinationsIid = typeof ( IApplicationDestinations ) . GUID ;
175
- var applicationDestinationsInstanceIid = typeof ( ApplicationDestinations ) . GUID ;
176
-
177
- PInvoke . CoCreateInstance (
178
- & applicationDestinationsInstanceIid ,
179
- null ,
180
- CLSCTX . CLSCTX_LOCAL_SERVER ,
181
- & applicationDestinationsIid ,
182
- ( void * * ) pApplicationDestinations . GetAddressOf ( ) )
183
- . ThrowOnFailure ( ) ;
184
-
185
- // Set App ID
186
- fixed ( char * pszAppId = "Microsoft.Windows.Explorer" )
187
- pApplicationDestinations . Get ( ) ->SetAppID ( pszAppId ) ;
188
-
189
- pApplicationDestinations . Get ( ) ->RemoveAllDestinations ( ) ;
154
+ PInvoke . SHAddToRecentDocs ( ( uint ) SHARD . SHARD_PIDL , null ) ;
190
155
191
156
return true ;
192
157
}
@@ -230,6 +195,20 @@ private unsafe bool UpdateRecentItems(bool isFolder)
230
195
if ( index is 20 )
231
196
break ;
232
197
198
+ // Exclude folders
199
+ if ( pShellItem . Get ( ) ->GetAttributes ( SFGAO_FLAGS . SFGAO_FOLDER , out var attribute ) == HRESULT . S_OK &&
200
+ ( attribute & SFGAO_FLAGS . SFGAO_FOLDER ) == SFGAO_FLAGS . SFGAO_FOLDER )
201
+ continue ;
202
+
203
+ // Exclude favorite files
204
+ using ComPtr < IShellItem2 > pShellItem2 = default ;
205
+ var shellItem2Iid = typeof ( IShellItem2 ) . GUID ;
206
+ hr = pShellItem . Get ( ) ->QueryInterface ( & shellItem2Iid , ( void * * ) pShellItem2 . GetAddressOf ( ) ) ;
207
+ hr = PInvoke . PSGetPropertyKeyFromName ( "System.Home.IsPinned" , out var propertyKey ) ;
208
+ hr = pShellItem2 . Get ( ) ->GetString ( propertyKey , out var szPropertyValue ) ;
209
+ if ( bool . TryParse ( szPropertyValue . ToString ( ) , out var isPinned ) && isPinned )
210
+ continue ;
211
+
233
212
// Get the target path
234
213
pShellItem . Get ( ) ->GetDisplayName ( SIGDN . SIGDN_DESKTOPABSOLUTEEDITING , out var szDisplayName ) ;
235
214
var targetPath = szDisplayName . ToString ( ) ;
@@ -241,21 +220,14 @@ private unsafe bool UpdateRecentItems(bool isFolder)
241
220
PInvoke . CoTaskMemFree ( szDisplayName . Value ) ;
242
221
243
222
// Strip the file extension except when the file name only contains extension (e.g. ".gitignore")
244
- if ( ! FoldersSettingsService . ShowFileExtensions )
245
- {
246
- string strippedExtension = SystemIO . Path . GetFileNameWithoutExtension ( fileName ) ;
247
- fileName = string . IsNullOrEmpty ( strippedExtension ) ? SystemIO . Path . GetFileName ( fileName ) : strippedExtension ;
248
- }
249
-
250
- // Get the PIDL
251
- PInvoke . SHGetIDListFromObject ( ( IUnknown * ) pShellItem . Get ( ) , out var pidl ) ;
223
+ if ( ! FoldersSettingsService . ShowFileExtensions &&
224
+ SystemIO . Path . GetFileNameWithoutExtension ( fileName ) is string fileNameWithoutExtension )
225
+ fileName = string . IsNullOrEmpty ( fileNameWithoutExtension ) ? SystemIO . Path . GetFileName ( fileName ) : fileNameWithoutExtension ;
252
226
253
227
// Get the date last modified
254
- using ComPtr < IShellItem2 > pShellItem2 = default ;
255
- var shellItem2Iid = typeof ( IShellItem2 ) . GUID ;
256
228
hr = pShellItem . Get ( ) ->QueryInterface ( & shellItem2Iid , ( void * * ) pShellItem2 . GetAddressOf ( ) ) ;
257
- hr = PInvoke . PSGetPropertyKeyFromName ( "System.DateCreated " , out var propertyKey ) ;
258
- hr = pShellItem2 . Get ( ) ->GetString ( propertyKey , out var szPropertyValue ) ;
229
+ hr = PInvoke . PSGetPropertyKeyFromName ( "System.DateModified " , out propertyKey ) ;
230
+ hr = pShellItem2 . Get ( ) ->GetString ( propertyKey , out szPropertyValue ) ;
259
231
if ( DateTime . TryParse ( szPropertyValue . ToString ( ) , out var lastModified ) )
260
232
lastModified = DateTime . MinValue ;
261
233
0 commit comments