From 41f214d5481491d9c703c818f29186f0ea9aa031 Mon Sep 17 00:00:00 2001 From: "seer-by-sentry[bot]" <157164994+seer-by-sentry[bot]@users.noreply.github.com> Date: Thu, 18 Sep 2025 15:00:36 +0000 Subject: [PATCH] Fix: Prevent crash when AutomaticDestinations directory is missing --- .../Widgets/QuickAccessWidgetViewModel.cs | 51 +++++++++++++++---- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/src/Files.App/ViewModels/UserControls/Widgets/QuickAccessWidgetViewModel.cs b/src/Files.App/ViewModels/UserControls/Widgets/QuickAccessWidgetViewModel.cs index 27dfb423e185..a8aaf8cd2e37 100644 --- a/src/Files.App/ViewModels/UserControls/Widgets/QuickAccessWidgetViewModel.cs +++ b/src/Files.App/ViewModels/UserControls/Widgets/QuickAccessWidgetViewModel.cs @@ -34,7 +34,7 @@ public sealed partial class QuickAccessWidgetViewModel : BaseWidgetViewModel, IW // Fields // TODO: Replace with IMutableFolder.GetWatcherAsync() once it gets implemented in IWindowsStorable - private readonly SystemIO.FileSystemWatcher _quickAccessFolderWatcher; + private readonly SystemIO.FileSystemWatcher? _quickAccessFolderWatcher; // Constructor @@ -46,19 +46,46 @@ public QuickAccessWidgetViewModel() PinToSidebarCommand = new AsyncRelayCommand(ExecutePinToSidebarCommand); UnpinFromSidebarCommand = new AsyncRelayCommand(ExecuteUnpinFromSidebarCommand); - _quickAccessFolderWatcher = new() + // Initialize FileSystemWatcher only if the AutomaticDestinations directory exists + var automaticDestinationsPath = SystemIO.Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Windows", "Recent", "AutomaticDestinations"); + if (SystemIO.Directory.Exists(automaticDestinationsPath)) { - Path = SystemIO.Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Windows", "Recent", "AutomaticDestinations"), - Filter = "f01b4d95cf55d32a.automaticDestinations-ms", - NotifyFilter = SystemIO.NotifyFilters.LastAccess | SystemIO.NotifyFilters.LastWrite | SystemIO.NotifyFilters.FileName - }; + try + { + _quickAccessFolderWatcher = new() + { + Path = automaticDestinationsPath, + Filter = "f01b4d95cf55d32a.automaticDestinations-ms", + NotifyFilter = SystemIO.NotifyFilters.LastAccess | SystemIO.NotifyFilters.LastWrite | SystemIO.NotifyFilters.FileName + }; - _quickAccessFolderWatcher.Changed += async (s, e) => - { - await RefreshWidgetAsync(); - }; + _quickAccessFolderWatcher.Changed += async (s, e) => + { + await RefreshWidgetAsync(); + }; - _quickAccessFolderWatcher.EnableRaisingEvents = true; + _quickAccessFolderWatcher.EnableRaisingEvents = true; + } + catch (SystemIO.DirectoryNotFoundException) + { + // Directory was deleted between the Exists check and FileSystemWatcher initialization + _quickAccessFolderWatcher = null; + } + catch (UnauthorizedAccessException) + { + // Access denied to the directory + _quickAccessFolderWatcher = null; + } + catch (ArgumentException) + { + // Invalid directory path + _quickAccessFolderWatcher = null; + } + } + else + { + _quickAccessFolderWatcher = null; + } } // Methods @@ -301,6 +328,8 @@ private void ExecuteOpenPropertiesCommand(WidgetFolderCardItem? item) public void Dispose() { + _quickAccessFolderWatcher?.Dispose(); + foreach (var item in Items) item.Dispose(); }