Ejemplo n.º 1
0
        }         // proc OnEndReadConfiguration

        #endregion

        #region -- Handle Events ----------------------------------------------------------

        private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            WatcherChangeTypes type;
            var e2 = e as RenamedEventArgs;

            // write debug message
            if (IsDebug)
            {
                if (e2 != null)
                {
                    Log.Debug("FileSystemWatcher event: Type={0}, Name={1} > {2}, FullPath={3} > {4}", e.ChangeType, e2.OldName, e.Name, e2.OldFullPath, e.FullPath);
                }
                else
                {
                    Log.Debug("FileSystemWatcher event: Type={0}, Name={1}, FullPath={2}", e.ChangeType, e.Name, e.FullPath);
                }
            }

            // we priorisize the events, and ignore lower level events, if the occure
            if ((e.ChangeType & WatcherChangeTypes.Created) != 0)             // file is created
            {
                type = WatcherChangeTypes.Created;

                lock (notifyQueue)
                {
                    RemoveNotifyEvent(e.FullPath);
                    notifyQueue.Add(new FileNotifyEvent(e));
                }
            }
            else if ((e.ChangeType & WatcherChangeTypes.Deleted) != 0)             // files is deleted
            {
                type = WatcherChangeTypes.Deleted;
                lock (notifyQueue)
                    RemoveNotifyEvent(e.FullPath);
            }
            else if ((e.ChangeType & WatcherChangeTypes.Renamed) != 0)             // files is renamed
            {
                type = WatcherChangeTypes.Renamed;

                lock (notifyQueue)
                {
                    RemoveNotifyEvent(e2.OldFullPath);

                    // check if the new name requires the filter criteria
                    if (Regex.IsMatch(e.Name, Procs.FileFilterToRegex(fileSystemWatcher.Filter)))
                    {
                        notifyQueue.Add(new FileNotifyEvent(e));
                    }
                }
            }
            else if ((e.ChangeType & WatcherChangeTypes.Changed) != 0)             // attributes or lastwrite changed
            {
                type = WatcherChangeTypes.Changed;

                lock (notifyQueue)
                {
                    RemoveNotifyEvent(e.FullPath);
                    notifyQueue.Add(new FileNotifyEvent(e));
                }
            }
            else
            {
                return;
            }

            // call a lua extension
            var member = this["NotifyFileChangeCore"];

            if (Lua.RtInvokeable(member))
            {
                try
                {
                    ((dynamic)this).NotifyFileChange(type, e);
                }
                catch (Exception ex)
                {
                    Log.Except(String.Format("NotifyFileChangeCore failed for {0}.", e.Name), ex);
                }
            }
        }         // event FileSystemWatcher_Changed