Skip to content

Commit ab94be3

Browse files
seer-by-sentry[bot]yaira2
authored andcommitted
Reliability: Only create file system watcher if AutomaticDestinations directory exists
1 parent a87929d commit ab94be3

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/Files.App/Services/Windows/WindowsRecentItemsService.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.Extensions.Logging;
55
using System.Collections.Specialized;
6+
using System.IO;
67
using System.Text;
78
using Windows.Win32;
89
using Windows.Win32.Foundation;
@@ -59,16 +60,23 @@ public IReadOnlyList<RecentItem> RecentFolders
5960

6061
public WindowsRecentItemsService()
6162
{
62-
_watcher = new()
63+
var automaticDestinationsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Recent), "AutomaticDestinations");
64+
65+
// Only create the file system watcher if the AutomaticDestinations directory exists
66+
if (Directory.Exists(automaticDestinationsPath))
6367
{
64-
Path = SystemIO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Recent), "AutomaticDestinations"),
65-
Filter = "5f7b5f1e01b83767.automaticDestinations-ms",
66-
NotifyFilter = SystemIO.NotifyFilters.DirectoryName | SystemIO.NotifyFilters.FileName | SystemIO.NotifyFilters.LastWrite,
67-
};
68-
69-
_watcher.Changed += Watcher_Changed;
70-
_watcher.Deleted += Watcher_Changed;
71-
_watcher.EnableRaisingEvents = true;
68+
_watcher = new()
69+
{
70+
Path = automaticDestinationsPath,
71+
Filter = "5f7b5f1e01b83767.automaticDestinations-ms",
72+
NotifyFilter = SystemIO.NotifyFilters.DirectoryName | SystemIO.NotifyFilters.FileName | SystemIO.NotifyFilters.LastWrite,
73+
};
74+
75+
_watcher.Changed += Watcher_Changed;
76+
_watcher.Deleted += Watcher_Changed;
77+
_watcher.EnableRaisingEvents = true;
78+
}
79+
// If the directory doesn't exist, _watcher remains null and the service will function without file system monitoring
7280
}
7381

7482
// Methods

0 commit comments

Comments
 (0)