Example #1
0
        public void WatchFile(string filePath)
        {
            bool   flag = false;
            string str  = PathHelper.GetDirectory(filePath);

            if (!FileChangeWatcher.DirectoryIsAccessible(str))
            {
                flag = true;
                str  = FileChangeWatcher.GetExistingParentDirectory(str);
            }
            if (string.IsNullOrEmpty(str))
            {
                return;
            }
            FileChangeWatcher.WatchedDirectory watchedDirectory;
            lock (this.syncLock)
            {
                if (!this.pathToWatchedDirectories.TryGetValue(str, out watchedDirectory))
                {
                    watchedDirectory = new FileChangeWatcher.WatchedDirectory(this, str);
                    this.pathToWatchedDirectories.Add(str, watchedDirectory);
                }
            }
            if (flag)
            {
                watchedDirectory.IncludeSubdirectories = flag;
            }
            watchedDirectory.WatchFile(filePath);
        }
Example #2
0
        private bool CanWatchChildren(FileChangeWatcher.WatchedDirectory watchedDirectory)
        {
            bool canWatchChildren = true;

            ErrorHandling.HandleBasicExceptions((Action)(() =>
            {
                if (PathHelper.DirectoryExists(watchedDirectory.Path) && Enumerable.Any <string>(Directory.EnumerateFileSystemEntries(watchedDirectory.Path)))
                {
                    return;
                }
                canWatchChildren = false;
            }), (Action <Exception>)(exception => canWatchChildren = false), new Func <Exception, bool>(ErrorHandling.BasicIOExceptionHandler));
            return(canWatchChildren);
        }
Example #3
0
        private void MoveToParentDirectory(FileChangeWatcher.WatchedDirectory watchedDirectory)
        {
            this.pathToWatchedDirectories.Remove(watchedDirectory.Path);
            string existingParentDirectory = FileChangeWatcher.GetExistingParentDirectory(watchedDirectory.Path);

            if (string.IsNullOrEmpty(existingParentDirectory))
            {
                return;
            }
            string[] strArray = Enumerable.ToArray <string>(watchedDirectory.FilePaths);
            if (!this.pathToWatchedDirectories.TryGetValue(existingParentDirectory, out watchedDirectory))
            {
                watchedDirectory = new FileChangeWatcher.WatchedDirectory(this, existingParentDirectory);
                this.pathToWatchedDirectories.Add(existingParentDirectory, watchedDirectory);
            }
            watchedDirectory.IncludeSubdirectories = true;
            foreach (string filePath in strArray)
            {
                watchedDirectory.WatchFile(filePath);
            }
        }
Example #4
0
 private void OnFileChanged(FileChangeWatcher.WatchedDirectory sender, FileSystemEventArgs eventArgs, bool raiseEvent)
 {
     if (raiseEvent && this.FileChanged != null)
     {
         this.FileChanged((object)this, eventArgs);
     }
     if (!Monitor.TryEnter(this.syncLock))
     {
         return;
     }
     try
     {
         if (this.CanWatchChildren(sender))
         {
             return;
         }
         this.MoveToParentDirectory(sender);
         sender.Dispose();
     }
     finally
     {
         Monitor.Exit(this.syncLock);
     }
 }