|
3 | 3 |
|
4 | 4 | using Microsoft.Extensions.Logging; |
5 | 5 | using System.Collections.Specialized; |
| 6 | +using System.IO; |
6 | 7 | using System.Text; |
7 | 8 | using Windows.Win32; |
8 | 9 | using Windows.Win32.Foundation; |
@@ -59,16 +60,23 @@ public IReadOnlyList<RecentItem> RecentFolders |
59 | 60 |
|
60 | 61 | public WindowsRecentItemsService() |
61 | 62 | { |
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)) |
63 | 67 | { |
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 |
72 | 80 | } |
73 | 81 |
|
74 | 82 | // Methods |
|
0 commit comments