Пример #1
0
        /// <summary>
        /// This function initializes the monitor by obtaining the current state and then creating and
        /// starting the monitor in a background thread, 
        /// </summary>
        public void EnableMonitoring()
        {
            if (this.Path.Equals(String.Empty)) return;

            initialSnapshot = GetFolderSnapshot();

            bckrndWorkerMonitor = new BackgroundWorker();
            bckrndWorkerMonitor.WorkerSupportsCancellation = true;
            bckrndWorkerMonitor.DoWork += new DoWorkEventHandler(onMonitorEvent);
            bckrndWorkerMonitor.RunWorkerAsync();
            log.Info("Background file watcher monitor process started.");
        }
Пример #2
0
        /// <summary>
        /// This function returns a current snapshot of the contents of the folder with 
        /// a timestamp showing when the snapshot was taken.
        /// </summary>
        /// <returns>snaphot of folder</returns>
        private DirectorySnapshot GetFolderSnapshot()
        {
            DirectorySnapshot snaphot = null;

            if (Directory.Exists(this.Path))
            {
                snaphot = new DirectorySnapshot(this.Path);
                foreach (string file in Directory.GetFiles(this.Path, this.Filter))
                    snaphot.FileList[file] = File.GetCreationTime(file);
                snaphot.tsRefreshed = DateTime.Now;
            }

            return snaphot;
        }
Пример #3
0
        /// <summary>
        /// This function compares two DirectorySnapshot objects and returns a list of newly added files
        /// </summary>
        /// <param name="initialSnapshot"></param>
        /// <param name="currentSnapshot"></param>
        /// <returns>List of new files</returns>
        private List<string> CompareSnaphots(DirectorySnapshot initialSnapshot, DirectorySnapshot currentSnapshot)
        {
            List<string> newFiles = new List<string>();

            if (initialSnapshot == null || currentSnapshot == null)
            {
                log.WarnFormat("FileSystemWatcherEnhanced cannot compare snapshots if snapshot is null.");
                return newFiles;
            } 

            lock (initialSnapshot)
            {
                lock (currentSnapshot)
                {
                    foreach (string file in currentSnapshot.FileList.Keys)
                        if (!initialSnapshot.FileList.ContainsKey(file))
                            newFiles.Add(file);
                }
            }
                
            return newFiles;
        }
Пример #4
0
        private void CheckForNewFiles()
        {
            //obtain current folder state                    
            currentSnapshot = GetFolderSnapshot();

            //compare current and initial states to determine new (i.e. unprocessed) files
            //Debug.Assert(initialSnapshot != null);
            //Debug.Assert(currentSnapshot != null);

            List<string> newFiles = CompareSnaphots(initialSnapshot, currentSnapshot);
            if (newFiles.Count > 0)
                log.InfoFormat("FileSystemWatcherEnhanced monitor found {0} files in {1}", newFiles.Count, this.Path);

            foreach (string file in newFiles)
            {
                log.WarnFormat("FileSystemWatcher monitor found an unprocessed file.  Launching delegate to process {0}", file);
                fileReceivedHandler.Invoke(this, new FileSystemEventArgs(WatcherChangeTypes.Created, this.Path, file.Replace(this.Path, "").TrimStart('\\')));
            }

            /// Reset network connection, if dropped
            if (!EnableRaisingEvents)
                EnableRaisingEvents = true;

            /// Reset the "initialState" after a certain period (typically daily) to help ensure we dont get a memory overflow
            if (initialSnapshot.tsRefreshed.AddMinutes(initialStateRefreshIntervalMins) < DateTime.Now)
                initialSnapshot = GetFolderSnapshot();
        }