11// Copyright (c) Files Community
22// Licensed under the MIT License.
33
4- using System ;
4+ using System . IO ;
55using System . Runtime . InteropServices ;
66using Windows . ApplicationModel ;
77using Windows . Win32 ;
@@ -26,6 +26,8 @@ public unsafe class JumpListManager : IDisposable
2626
2727 private readonly static string _aumid = $ "{ Package . Current . Id . FamilyName } !App";
2828
29+ private FileSystemWatcher ? _watcher ;
30+
2931 public bool SyncWithExplorerJumpList ( int maxCount = 30 )
3032 {
3133 ClearAutomaticDestinations ( ) ;
@@ -47,12 +49,36 @@ public bool SyncWithExplorerJumpList(int maxCount = 30)
4749 return true ;
4850 }
4951
50- public void WatchExplorerJumpListChanges ( )
52+ public bool WatchExplorerJumpListChanges ( )
5153 {
5254 // Get the path to the Explorer's automatic destinations file
5355 using ComHeapPtr < char > pwszRecentFolderPath = default ;
5456 PInvoke . SHGetKnownFolderPath ( FOLDERID . FOLDERID_Recent , KNOWN_FOLDER_FLAG . KF_FLAG_DONT_VERIFY | KNOWN_FOLDER_FLAG . KF_FLAG_NO_ALIAS , HANDLE . Null , ( PWSTR * ) pwszRecentFolderPath . GetAddressOf ( ) ) ;
5557 var path = $ "{ new ( pwszRecentFolderPath . Get ( ) ) } \\ AutomaticDestinations";
58+
59+ // NOTE: Since SHChangeNotifyRegister doesn't notify us of the changes made to the contents of a file, we stick to ReadDirectoryChange for this scenario
60+ _watcher ? . Dispose ( ) ;
61+ _watcher = new ( )
62+ {
63+ Path = path ,
64+ Filter = "f01b4d95cf55d32a.automaticDestinations-ms" , // Explorer's automatic destinations file
65+ NotifyFilter = NotifyFilters . LastWrite | NotifyFilters . FileName | NotifyFilters . CreationTime ,
66+ } ;
67+
68+ _watcher . Changed += Watcher_Changed ;
69+
70+ try
71+ {
72+ // This may throw various exceptions (e.g., when the file doesn't exist or cannot be accessed)
73+ _watcher . EnableRaisingEvents = true ;
74+ }
75+ catch
76+ {
77+ // Gracefully exit if we can't monitor the file
78+ return false ;
79+ }
80+
81+ return true ;
5682 }
5783
5884 private bool ClearAutomaticDestinations ( )
@@ -235,14 +261,19 @@ private bool CopyToFilesAutomaticDestinations(IObjectCollection* pRecentOC, IObj
235261 return true ;
236262 }
237263
238- private void Watcher_FileChanged ( )
264+ private void Watcher_Changed ( object sender , FileSystemEventArgs e )
239265 {
240266 // Sync the jump list when the Explorer's automatic destinations file changes
241267 SyncWithExplorerJumpList ( ) ;
242268 }
243269
244270 public void Dispose ( )
245271 {
272+ if ( _watcher is not null )
273+ {
274+ _watcher . EnableRaisingEvents = false ;
275+ _watcher . Dispose ( ) ;
276+ }
246277 }
247278 }
248279}
0 commit comments