PowerToys/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs

53 lines
1.6 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.IO;
using System.Threading;
using Wox.Infrastructure.Logger;
2016-01-06 14:45:08 +08:00
namespace Wox.Plugin.Program
{
internal class FileChangeWatcher
{
2016-01-07 05:34:42 +08:00
private static bool isIndexing;
private static List<string> watchedPath = new List<string>();
public static void AddWatch(string path, bool includingSubDirectory = true)
{
if (watchedPath.Contains(path)) return;
if (!Directory.Exists(path))
{
Log.Warn($"FileChangeWatcher: {path} doesn't exist");
return;
}
watchedPath.Add(path);
foreach (string fileType in ProgramStorage.Instance.ProgramSuffixes.Split(';'))
{
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)
{
ThreadPool.QueueUserWorkItem(o =>
{
Programs.IndexPrograms();
isIndexing = false;
});
}
}
}
}