/// ----------------------------------------------------------------------------- /// <summary> /// Removes a watched file from the collection /// </summary> /// <param name="ObjectPath">The key to remove from the collection</param> /// <remarks></remarks> /// <history> /// [michaelpi] 6/4/2004 Created /// </history> /// ----------------------------------------------------------------------------- public static void RemoveWatchedFile(string ObjectPath) { WatchedObject watchedObject = null; if (_watchedObjects.ContainsKey(ObjectPath)) { watchedObject = _watchedObjects[ObjectPath]; if (watchedObject == null) { throw (new Exception( string.Format("Unable to retreive the object from the collection using path {0}", ObjectPath))); } var watcher = watchedObject.Watcher; watcher.Created -= OnFileCreated; watcher.Changed -= OnFileChanged; watcher.Deleted -= OnFileDeleted; watcher.EnableRaisingEvents = false; watcher.Dispose(); _watchedObjects.Remove(ObjectPath); } else { throw (new Exception(string.Format("The item {0} does not exist in the collection", ObjectPath))); } }
/// ----------------------------------------------------------------------------- /// <overloads> /// Adds a watched object into the collection /// </overloads> /// <summary> /// Adds a watched object into the collection /// </summary> /// <param name="ObjectPath">The path of the item</param> /// <param name="Filter"></param> /// <param name="IncludeSubdirectories"></param> /// <remarks></remarks> /// <history> /// [michaelpi] 6/4/2004 Created /// [karunk] 6/28/2004 Changed the NotifyFilter so that /// events get fired properly for Creation, /// Deletion and Change. /// </history> /// ----------------------------------------------------------------------------- public static void AddWatchedObject(string ObjectPath) { WatchedObject watchedObject = null; var watcher = new FileSystemWatcher(); watcher.Path = new FileInfo(ObjectPath).DirectoryName; watcher.Filter = Path.GetFileName(ObjectPath); watcher.IncludeSubdirectories = false; watcher.NotifyFilter = NotifyFilters.LastWrite; //----------------------------------------------------------------------------- // Set the Handlers //----------------------------------------------------------------------------- watcher.Changed += OnFileChanged; watcher.Deleted += OnFileDeleted; watcher.Created += OnFileCreated; watcher.EnableRaisingEvents = true; watchedObject = new WatchedObject(ObjectType.File, watcher); _watchedObjects.Add(ObjectPath, watchedObject); }