Exemple #1
0
 public FileSystemStalker(Folder folder, FileChangeTypes fileChangeTypes, FileChangedDelegate changedDelegate)
 {
     this.changedDelegate = changedDelegate;
     if (!folder.Exists()) return;
     watcher = new FileSystemWatcher(folder.FullName) {IncludeSubdirectories = true, EnableRaisingEvents = true};
     if (AskedForEvent(fileChangeTypes, FileChangeTypes.Created)) watcher.Created += RaiseEvent;
     if (AskedForEvent(fileChangeTypes, FileChangeTypes.Changed)) watcher.Changed += RaiseEvent;
     if (AskedForEvent(fileChangeTypes, FileChangeTypes.Deleted)) watcher.Deleted += RaiseEvent;
     if (AskedForEvent(fileChangeTypes, FileChangeTypes.Renamed)) watcher.Renamed += RaiseEvent;
 }
        private void RaiseFileChanged(String fullPath, FileChangedDelegate action)
        {
            FileStream input;

            try
            {
                input = File.OpenRead(fullPath);
            }
            catch (Exception ex)
            {
                HandleError(ex);

                if (!File.Exists(fullPath))
                {
                    _changed.Remove(fullPath);
                }
                return;
            }

            _changed.Remove(fullPath);

            fullPath = PrepareFullPath(fullPath);
            using (input)
            {
                NotDisposableStream stream = new NotDisposableStream(input);
                foreach (FileChangedDelegate callback in action.Enumerate())
                {
                    try
                    {
                        input.Position = 0;
                        callback(fullPath, stream);
                    }
                    catch (Exception ex)
                    {
                        HandleError(ex);
                    }
                }
            }
        }