PowerToys/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs
bao-qian 705354a3d6 Better logger
1. Throw exception for fatal/error log when debugging
2. Write to debug output for warn/debug/info log when debugging
3. part of #355
2015-11-07 17:32:58 +00:00

53 lines
1.6 KiB
C#

using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using Wox.Infrastructure.Logger;
namespace Wox.Plugin.Program
{
internal class FileChangeWatcher
{
private static bool isIndexing = false;
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;
});
}
}
}
}