예제 #1
0
 public void Add(string file, Action action)
 {
     _files[file] = new WatchFile()
     {
         LastModified = System.IO.File.GetLastWriteTime(file),
         OnChange     = action
     };
 }
예제 #2
0
        public FileWatcher(Lifetime lifetime, string directory, string fileMask, NotifyFilters notifyFilter, WatchFile watchFile)
        {
            OnWatchedEvent = new Signal <FileSystemEventArgs>(lifetime,
                                                              "FileWatcher.OnWatchedEvent");

            var watcher = new FileSystemWatcher()
            {
                Path         = directory,
                Filter       = fileMask,
                NotifyFilter = notifyFilter
            };

            var handler = new FileSystemEventHandler(OnChanged);

            switch (watchFile)
            {
            case WatchFile.Create:
                lifetime.AddBracket(() => watcher.Created += handler, () => watcher.Created -= handler);
                break;

            case WatchFile.Change:
                lifetime.AddBracket(() => watcher.Changed += handler, () => watcher.Changed -= handler);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(watchFile), watchFile, null);
            }
        }