Skip to content

Commit 41f214d

Browse files
seer-by-sentry[bot]yaira2
authored andcommitted
Fix: Prevent crash when AutomaticDestinations directory is missing
1 parent a87929d commit 41f214d

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

src/Files.App/ViewModels/UserControls/Widgets/QuickAccessWidgetViewModel.cs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public sealed partial class QuickAccessWidgetViewModel : BaseWidgetViewModel, IW
3434
// Fields
3535

3636
// TODO: Replace with IMutableFolder.GetWatcherAsync() once it gets implemented in IWindowsStorable
37-
private readonly SystemIO.FileSystemWatcher _quickAccessFolderWatcher;
37+
private readonly SystemIO.FileSystemWatcher? _quickAccessFolderWatcher;
3838

3939
// Constructor
4040

@@ -46,19 +46,46 @@ public QuickAccessWidgetViewModel()
4646
PinToSidebarCommand = new AsyncRelayCommand<WidgetFolderCardItem>(ExecutePinToSidebarCommand);
4747
UnpinFromSidebarCommand = new AsyncRelayCommand<WidgetFolderCardItem>(ExecuteUnpinFromSidebarCommand);
4848

49-
_quickAccessFolderWatcher = new()
49+
// Initialize FileSystemWatcher only if the AutomaticDestinations directory exists
50+
var automaticDestinationsPath = SystemIO.Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Windows", "Recent", "AutomaticDestinations");
51+
if (SystemIO.Directory.Exists(automaticDestinationsPath))
5052
{
51-
Path = SystemIO.Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Windows", "Recent", "AutomaticDestinations"),
52-
Filter = "f01b4d95cf55d32a.automaticDestinations-ms",
53-
NotifyFilter = SystemIO.NotifyFilters.LastAccess | SystemIO.NotifyFilters.LastWrite | SystemIO.NotifyFilters.FileName
54-
};
53+
try
54+
{
55+
_quickAccessFolderWatcher = new()
56+
{
57+
Path = automaticDestinationsPath,
58+
Filter = "f01b4d95cf55d32a.automaticDestinations-ms",
59+
NotifyFilter = SystemIO.NotifyFilters.LastAccess | SystemIO.NotifyFilters.LastWrite | SystemIO.NotifyFilters.FileName
60+
};
5561

56-
_quickAccessFolderWatcher.Changed += async (s, e) =>
57-
{
58-
await RefreshWidgetAsync();
59-
};
62+
_quickAccessFolderWatcher.Changed += async (s, e) =>
63+
{
64+
await RefreshWidgetAsync();
65+
};
6066

61-
_quickAccessFolderWatcher.EnableRaisingEvents = true;
67+
_quickAccessFolderWatcher.EnableRaisingEvents = true;
68+
}
69+
catch (SystemIO.DirectoryNotFoundException)
70+
{
71+
// Directory was deleted between the Exists check and FileSystemWatcher initialization
72+
_quickAccessFolderWatcher = null;
73+
}
74+
catch (UnauthorizedAccessException)
75+
{
76+
// Access denied to the directory
77+
_quickAccessFolderWatcher = null;
78+
}
79+
catch (ArgumentException)
80+
{
81+
// Invalid directory path
82+
_quickAccessFolderWatcher = null;
83+
}
84+
}
85+
else
86+
{
87+
_quickAccessFolderWatcher = null;
88+
}
6289
}
6390

6491
// Methods
@@ -301,6 +328,8 @@ private void ExecuteOpenPropertiesCommand(WidgetFolderCardItem? item)
301328

302329
public void Dispose()
303330
{
331+
_quickAccessFolderWatcher?.Dispose();
332+
304333
foreach (var item in Items)
305334
item.Dispose();
306335
}

0 commit comments

Comments
 (0)