@@ -34,7 +34,7 @@ public sealed partial class QuickAccessWidgetViewModel : BaseWidgetViewModel, IW
34
34
// Fields
35
35
36
36
// TODO: Replace with IMutableFolder.GetWatcherAsync() once it gets implemented in IWindowsStorable
37
- private readonly SystemIO . FileSystemWatcher _quickAccessFolderWatcher ;
37
+ private readonly SystemIO . FileSystemWatcher ? _quickAccessFolderWatcher ;
38
38
39
39
// Constructor
40
40
@@ -46,19 +46,46 @@ public QuickAccessWidgetViewModel()
46
46
PinToSidebarCommand = new AsyncRelayCommand < WidgetFolderCardItem > ( ExecutePinToSidebarCommand ) ;
47
47
UnpinFromSidebarCommand = new AsyncRelayCommand < WidgetFolderCardItem > ( ExecuteUnpinFromSidebarCommand ) ;
48
48
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 ) )
50
52
{
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
+ } ;
55
61
56
- _quickAccessFolderWatcher . Changed += async ( s , e ) =>
57
- {
58
- await RefreshWidgetAsync ( ) ;
59
- } ;
62
+ _quickAccessFolderWatcher . Changed += async ( s , e ) =>
63
+ {
64
+ await RefreshWidgetAsync ( ) ;
65
+ } ;
60
66
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
+ }
62
89
}
63
90
64
91
// Methods
@@ -301,6 +328,8 @@ private void ExecuteOpenPropertiesCommand(WidgetFolderCardItem? item)
301
328
302
329
public void Dispose ( )
303
330
{
331
+ _quickAccessFolderWatcher ? . Dispose ( ) ;
332
+
304
333
foreach ( var item in Items )
305
334
item . Dispose ( ) ;
306
335
}
0 commit comments