Skip to content

Commit 813d167

Browse files
committed
Update
1 parent c773727 commit 813d167

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/Files.App.Storage/Windows/Managers/JumpListManager.cs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4-
using System;
4+
using System.IO;
55
using System.Runtime.InteropServices;
66
using Windows.ApplicationModel;
77
using 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

Comments
 (0)