/ src / modules / launcher / Plugins / Microsoft.Plugin.Program / Storage / Win32ProgramFileSystemWatchers.cs
Win32ProgramFileSystemWatchers.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Collections.Generic;
 7  using System.IO;
 8  using System.Linq;
 9  
10  using Wox.Infrastructure.Storage;
11  using Wox.Plugin.Logger;
12  
13  namespace Microsoft.Plugin.Program.Storage
14  {
15      internal class Win32ProgramFileSystemWatchers : IDisposable
16      {
17          public string[] PathsToWatch { get; set; }
18  
19          public List<FileSystemWatcherWrapper> FileSystemWatchers { get; set; }
20  
21          private bool _disposed;
22  
23          // This class contains the list of directories to watch and initializes the File System Watchers
24          public Win32ProgramFileSystemWatchers()
25          {
26              PathsToWatch = GetPathsToWatch();
27              SetFileSystemWatchers();
28          }
29  
30          // Returns an array of paths to be watched
31          private static string[] GetPathsToWatch()
32          {
33              string[] paths = new string[]
34                              {
35                                 Environment.GetFolderPath(Environment.SpecialFolder.StartMenu),
36                                 Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu),
37                                 Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
38                                 Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory),
39                              };
40  
41              var invalidPaths = new List<string>();
42              foreach (var path in paths)
43              {
44                  try
45                  {
46                      Directory.GetFiles(path);
47                  }
48                  catch (Exception ex)
49                  {
50                      Log.Exception($"Failed to get files in {path}", ex, typeof(Win32ProgramFileSystemWatchers));
51                      invalidPaths.Add(path);
52                  }
53              }
54  
55              return paths.Except(invalidPaths).ToArray();
56          }
57  
58          // Initializes the FileSystemWatchers
59          private void SetFileSystemWatchers()
60          {
61              FileSystemWatchers = new List<FileSystemWatcherWrapper>();
62              for (int index = 0; index < PathsToWatch.Length; index++)
63              {
64                  FileSystemWatchers.Add(new FileSystemWatcherWrapper());
65              }
66          }
67  
68          public void Dispose()
69          {
70              Dispose(disposing: true);
71              GC.SuppressFinalize(this);
72          }
73  
74          protected virtual void Dispose(bool disposing)
75          {
76              if (!_disposed)
77              {
78                  if (disposing)
79                  {
80                      for (int index = 0; index < PathsToWatch.Length; index++)
81                      {
82                          FileSystemWatchers[index].Dispose();
83                      }
84  
85                      _disposed = true;
86                  }
87              }
88          }
89      }
90  }