PowerToys/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Storage/Win32ProgramFileSystemWatchers.cs

66 lines
2.0 KiB
C#
Raw Normal View History

Functionality to detect Win32 apps which are installed, deleted or renamed while PowerToys is running (#4960) * Added file system wrapper and interface * added win32program repository which would load store app and also handle new apps being added/deleted * Added event handlers to win32 program repo * added paths to monitor and setting FSWs for each location * Events firing as expected * filter extensions added, events fire as expected * override gethashcode so that duplicates don't show up. OnCreated and OnDeleted events trigger as expected * implemented setter for filters in FileSystemWatcher * Rename adds item but does not seem to delete the previous app * catching an exception when a duplicate item is inserted * Removed notify filter for directory because we only need to monitor files * Added exe programs to be indexed in the desktop and startmenu * created a new class to init FileSystemHelpers instead of main * Added fix for shortcut applications to work as expected while renaming and deleting them * Added wrappers for file system operations * Added some tests * Added all tests for appref-ms and added a condition to search in sub directories * Added tests for Exe applications * Added lnk app tests * Added tests for shortcut applications * removed unnecessary wrappers * override Equals for win32 * removed debug statements * Fixed exe issue * Fixed internet shortcut exception * fixed renaming shortcut apps * Added a retry block for ReadAllLines * capitalized method name - RetrieveTargetPath * made naming consistent, helper variables start with underscore * Added the exception condition back * renamed Win32ProgramRepositoryHelper to Win32ProgramFileSystemWatchers * made win32Program repository variable static * changed list to ilist * disposed file system watchers * make retrieveTargetPath upper case in tests
2020-07-18 13:32:21 +08:00
using System;
using System.Collections.Generic;
using Wox.Infrastructure.Storage;
namespace Microsoft.Plugin.Program.Storage
{
internal class Win32ProgramFileSystemWatchers : IDisposable
{
public readonly string[] _pathsToWatch;
public List<FileSystemWatcherWrapper> _fileSystemWatchers;
private bool _disposed = false;
// This class contains the list of directories to watch and initializes the File System Watchers
public Win32ProgramFileSystemWatchers()
{
_pathsToWatch = GetPathsToWatch();
SetFileSystemWatchers();
}
// Returns an array of paths to be watched
private string[] GetPathsToWatch()
{
string[] paths = new string[]
{
Environment.GetFolderPath(Environment.SpecialFolder.Programs),
Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms),
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
};
return paths;
}
// Initializes the FileSystemWatchers
private void SetFileSystemWatchers()
{
_fileSystemWatchers = new List<FileSystemWatcherWrapper>();
for (int index = 0; index < _pathsToWatch.Length; index++)
{
_fileSystemWatchers.Add(new FileSystemWatcherWrapper());
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
for(int index = 0; index < _pathsToWatch.Length; index++)
{
_fileSystemWatchers[index].Dispose();
}
_disposed = true;
}
}
}
}
}