2015-01-03 15:20:34 +08:00
|
|
|
|
using System.Collections.Generic;
|
2014-08-14 22:21:07 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
2016-05-06 04:15:13 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2015-11-08 01:32:58 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2016-01-06 14:45:08 +08:00
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
namespace Wox.Plugin.Program
|
2014-08-14 22:21:07 +08:00
|
|
|
|
{
|
|
|
|
|
internal class FileChangeWatcher
|
|
|
|
|
{
|
2016-01-07 05:34:42 +08:00
|
|
|
|
private static bool isIndexing;
|
2014-08-14 22:21:07 +08:00
|
|
|
|
private static List<string> watchedPath = new List<string>();
|
|
|
|
|
|
2016-03-28 10:09:57 +08:00
|
|
|
|
public static void AddWatch(string path, string[] programSuffixes, bool includingSubDirectory = true)
|
2014-08-14 22:21:07 +08:00
|
|
|
|
{
|
|
|
|
|
if (watchedPath.Contains(path)) return;
|
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
Log.Warn($"FileChangeWatcher: {path} doesn't exist");
|
2014-08-14 22:21:07 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
watchedPath.Add(path);
|
2016-03-28 10:09:57 +08:00
|
|
|
|
foreach (string fileType in programSuffixes)
|
2014-08-14 22:21:07 +08:00
|
|
|
|
{
|
|
|
|
|
FileSystemWatcher watcher = new FileSystemWatcher
|
|
|
|
|
{
|
|
|
|
|
Path = path,
|
|
|
|
|
IncludeSubdirectories = includingSubDirectory,
|
|
|
|
|
Filter = string.Format("*.{0}", fileType),
|
|
|
|
|
EnableRaisingEvents = true
|
|
|
|
|
};
|
|
|
|
|
watcher.Changed += FileChanged;
|
|
|
|
|
watcher.Created += FileChanged;
|
|
|
|
|
watcher.Deleted += FileChanged;
|
|
|
|
|
watcher.Renamed += FileChanged;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void FileChanged(object source, FileSystemEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!isIndexing)
|
|
|
|
|
{
|
2016-05-06 04:15:13 +08:00
|
|
|
|
Task.Run(() =>
|
2014-08-14 22:21:07 +08:00
|
|
|
|
{
|
2016-05-07 10:55:09 +08:00
|
|
|
|
Main.IndexPrograms();
|
2014-08-14 22:21:07 +08:00
|
|
|
|
isIndexing = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|