Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -46,19 +46,46 @@ public QuickAccessWidgetViewModel()
PinToSidebarCommand = new AsyncRelayCommand<WidgetFolderCardItem>(ExecutePinToSidebarCommand);
UnpinFromSidebarCommand = new AsyncRelayCommand<WidgetFolderCardItem>(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
Expand Down Expand Up @@ -301,6 +328,8 @@ private void ExecuteOpenPropertiesCommand(WidgetFolderCardItem? item)

public void Dispose()
{
_quickAccessFolderWatcher?.Dispose();

foreach (var item in Items)
item.Dispose();
}
Expand Down
Loading