示例#1
0
        public DirectoryMonitor(IConfiguration configuration, IChannel channel, IFileSystem fileSystem)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            if (channel == null)
            {
                throw new ArgumentNullException(nameof(channel));
            }
            if (fileSystem == null)
            {
                throw new ArgumentNullException(nameof(fileSystem));
            }

            _channel       = channel;
            _fileSystem    = fileSystem;
            _directoryPath = configuration["directory-path"];

            _watcher = _fileSystem.FileSystemWatcher.FromPath(_directoryPath);
            _watcher.IncludeSubdirectories = true;
            _watcher.Created += GetEventAction(FileAction.Create);
            _watcher.Changed += GetEventAction(FileAction.Change);
            _watcher.Deleted += GetEventAction(FileAction.Delete);
            _watcher.Renamed += (sender, args) =>
            {
                _channel.AddFile(new RenameFileModel
                {
                    FileAction   = FileAction.Rename,
                    RelativePath = GetRelativePath(args.OldFullPath, _directoryPath),
                    NewFileName  = GetRelativePath(args.FullPath, _directoryPath)
                });
            };
        }
示例#2
0
 private void SendFile(string absolutePath, FileAction action)
 {
     _channel.AddFile(new FileModel
     {
         RelativePath = GetRelativePath(absolutePath, _directoryPath),
         FileAction   = action,
         FullPath     = absolutePath
     });
 }