/// <summary> /// 当一个监视项改变时,调用对应处理函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void fsw_Changed(object sender, FileSystemEventArgs e) { Debug.WriteLine(e.FullPath + " file changed"); FileChangedEventHandler handler = (FileChangedEventHandler)this.m_watchers[sender]; string fullPath = Path.GetFullPath(e.FullPath); FileChangedEventArgs args = new FileChangedEventArgs(fullPath, m_keys[fullPath.ToLower()]); if (handler != null) { try { if (handler.Method.IsStatic) { handler.Method.Invoke(null, new object[] { this, args }); } else { handler.Method.Invoke(handler.Target, new object[] { this, args }); } } catch (System.Reflection.TargetInvocationException tiEx) { Debug.WriteLine(tiEx.InnerException.ToString()); throw tiEx; } } else { this.m_watchers.Remove(sender); } }
private FileWatcher CreateFileWatcher(string directoryPath = null, bool includeSubdirectories = false, IEnumerable <Regex> filters = null, FileChangedEventHandler fileChangedEventHandler = null) { return(new FileWatcher(directoryPath ?? "dummyDirectoryPath", includeSubdirectories, filters ?? new[] { new Regex(".") }, fileChangedEventHandler ?? DummyFileChangedHandler)); }
/// <summary> /// 添加一个监视项 /// </summary> /// <param name="key">标识键</param> /// <param name="fileName">文件名</param> /// <param name="changedHandler">文件改变处理函数</param> public void AddItem(object key, string fileName, FileChangedEventHandler changedHandler) { FileSystemWatcher fsw = new FileSystemWatcher(Path.GetDirectoryName(fileName), Path.GetFileName(fileName)); fsw.Changed += new FileSystemEventHandler(fsw_Changed); fsw.NotifyFilter = NotifyFilters.LastAccess; fsw.EnableRaisingEvents = true; m_watchers.Add(fsw, changedHandler); m_keys.Add(Path.GetFullPath(fileName).ToLower(), key); }
/// <inheritdoc /> public IFileWatcher Create(string directoryPath, bool includeSubdirectories, IEnumerable <string> fileNamePatterns, FileChangedEventHandler fileChangedEventHandler) { directoryPath = ResolveDirectoryPath(directoryPath, _nodeJSProcessOptions); ReadOnlyCollection <Regex> filters = ResolveFilters(fileNamePatterns); return(new FileWatcher(directoryPath, includeSubdirectories, filters, fileChangedEventHandler)); }
public CodeDocument(CodeDescriptor desc, DocumentView parent) : base(desc.FilePath, parent) { this.desc = desc; Text = desc.Name; BackColor = UIManager.Instance.DullFlairColor; initControls(); configureControls(); addControls(); discHandler = new FileChangedEventHandler(discListener); Dirty = false; }
/// <summary> /// Creates a <see cref="FileWatcher"/>. /// </summary> /// <param name="directoryPath">The path of the directory to watch for file changes.</param> /// <param name="includeSubdirectories">The value specifying whether to watch subdirectories of <paramref name="directoryPath"/>.</param> /// <param name="filters">The filters for file names to watch.</param> /// <param name="fileChangedEventHandler">The method that will handle file changed events.</param> /// <exception cref="ArgumentException">Thrown if <paramref name="directoryPath"/> is <c>null</c>, whitespace or an empty string.</exception> /// <exception cref="ArgumentNullException">Thrown if <paramref name="filters"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">Thrown if <paramref name="fileChangedEventHandler"/> is <c>null</c>.</exception> /// <exception cref="ArgumentException">Thrown if <paramref name="filters"/> is empty.</exception> public FileWatcher(string directoryPath, bool includeSubdirectories, IEnumerable <Regex> filters, FileChangedEventHandler fileChangedEventHandler) { if (string.IsNullOrWhiteSpace(directoryPath)) { throw new ArgumentException(Strings.ArgumentException_Shared_ValueCannotBeNullWhitespaceOrAnEmptyString, nameof(directoryPath)); } _directoryPath = directoryPath; _includeSubdirectories = includeSubdirectories; _filters = filters ?? throw new ArgumentNullException(nameof(filters)); if (!_filters.Any()) { throw new ArgumentException(Strings.ArgumentException_Shared_ValueCannotBeEmpty, nameof(filters)); } _fileChangedEventHandler += fileChangedEventHandler ?? throw new ArgumentNullException(nameof(fileChangedEventHandler)); _notifyFilters = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; }
private void HandleAddRemoveFiles(FileChangeType changeType, int cProjects, int cFiles, IVsProject[] rgpProjects, int[] rgFirstIndices, string[] rgpszMkDocuments, FileChangedEventHandler csFileEvent, FileChangedEventHandler templateEvent) { if (csFileEvent == null && templateEvent == null) { return; } var paths = ExtractPath(cProjects, cFiles, rgpProjects, rgFirstIndices, rgpszMkDocuments); var templates = ImmutableArray.CreateBuilder<string>(); var csFiles = ImmutableArray.CreateBuilder<string>(cFiles); foreach (var path in paths) { if (path.EndsWith(Constants.CsExtension, StringComparison.InvariantCultureIgnoreCase)) { csFiles.Add(path); } else if (path.EndsWith(Constants.TemplateExtension, StringComparison.InvariantCultureIgnoreCase)) { templates.Add(path); } } if (csFileEvent != null && csFiles.Count > 0) { csFileEvent(this, new FileChangedEventArgs(changeType, csFiles.ToArray())); } if (templateEvent != null && templates.Count > 0) { templateEvent(this, new FileChangedEventArgs(changeType, templates.ToArray())); } }
private void HandleAddRemoveFiles(FileChangeType changeType, int cProjects, int cFiles, IVsProject[] rgpProjects, int[] rgFirstIndices, string[] rgpszMkDocuments, FileChangedEventHandler csFileEvent, FileChangedEventHandler templateEvent) { if (csFileEvent == null && templateEvent == null) { return; } var paths = ExtractPath(cProjects, cFiles, rgpProjects, rgFirstIndices, rgpszMkDocuments); var templates = ImmutableArray.CreateBuilder <string>(); var csFiles = ImmutableArray.CreateBuilder <string>(cFiles); foreach (var path in paths) { if (path.EndsWith(Constants.CsExtension, StringComparison.InvariantCultureIgnoreCase)) { csFiles.Add(path); } else if (path.EndsWith(Constants.TemplateExtension, StringComparison.InvariantCultureIgnoreCase)) { templates.Add(path); } } if (csFileEvent != null && csFiles.Count > 0) { csFileEvent(this, new FileChangedEventArgs(changeType, csFiles.ToArray())); } if (templateEvent != null && templates.Count > 0) { templateEvent(this, new FileChangedEventArgs(changeType, templates.ToArray())); } }
private void Dispose(bool disposeManagedResources) { if (!disposeManagedResources) return; var disposedAlready = Interlocked.Exchange(ref _alreadyDisposed, 1); if (disposedAlready != 0) { return; } // Dispose managed resources. _fileSystemWatcher.Changed -= _fileSystemWatcherChangedEvent; _fileSystemWatcher.Created -= _fileSystemWatcherCreatedEvent; _fileSystemWatcher.Deleted -= _fileSystemWatcherDeletedEvent; _fileSystemWatcher.Renamed -= _fileSystemWatcherRenamedEvent; _fileSystemWatcher.Dispose(); _fileChangedEvent = null; _fileCreatedEvent = null; _fileDeletedEvent = null; _fileRenamedEvent = null; _fileFinishedChangingEvent = null; // Dispose unmanaged resources. // If it is available, make the call to the // base class's Dispose(Boolean) method }
public FileHandlers(string uniquePath, SharedWatcher sharedWatcher, FileChangedEventHandler handler, LogLevel logLevel) : base(uniquePath, sharedWatcher, subscribeToChanged: true) { _handler = handler; _logLevel = logLevel; }