Пример #1
0
        public void SubscribeWatcher(List <FileSystemSafeWatcher> watchers, FileSystemEventHandler changedE, FileSystemEventHandler DeleteE, RenamedEventHandler RenameE)
        {
            List <ItemPath> paths             = Utility.GetListMapPath(Constants.MAPPING_CLIENT_FILENAME);
            var             checkSourceUnique = new List <string>();

            foreach (ItemPath item in paths)
            {
                if (!checkSourceUnique.Contains(item.Source))
                {
                    checkSourceUnique.Add(item.Source);

                    FileSystemSafeWatcher watcher = new FileSystemSafeWatcher();
                    watcher.IncludeSubdirectories = false;
                    watcher.Path         = item.Source;
                    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                           | NotifyFilters.FileName | NotifyFilters.DirectoryName;;
                    watcher.Filter   = "*.*";
                    watcher.Created += changedE;
                    //  watcher.Changed += changedE;
                    //  watcher.Deleted += DeleteE;
                    watcher.Renamed            += RenameE;
                    watcher.EnableRaisingEvents = true;

                    watchers.Add(watcher);
                }
            }

            log.Info("Subscribe success: " + paths.Count + " path.");
        }
Пример #2
0
        public void SubscribeWatcher(List <FileSystemSafeWatcher> watchers, FileSystemEventHandler changedE, FileSystemEventHandler DeleteE, RenamedEventHandler RenameE)
        {
            List <ItemPath> paths = Utility.GetListMapPath(Constants.MAPPING_SERVER_FILENAME);

            foreach (ItemPath item in paths)
            {
                if (!Directory.Exists(item.Source) || !Directory.Exists(item.Destination))
                {
                    if (!Directory.Exists(item.Source))
                    {
                        log.Error("Not exist: " + item.Source);
                    }

                    if (!Directory.Exists(item.Destination))
                    {
                        log.Error("Not exist: " + item.Destination);
                    }
                    continue;
                }

                FileSystemSafeWatcher watcher = new FileSystemSafeWatcher();
                watcher.IncludeSubdirectories = true;
                watcher.Path         = item.Source;
                watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                       | NotifyFilters.FileName | NotifyFilters.DirectoryName;;

                watcher.Filter              = "*.*";
                watcher.Created            += changedE;
                watcher.Deleted            += DeleteE;
                watcher.Renamed            += RenameE;
                watcher.EnableRaisingEvents = true;

                watchers.Add(watcher);
            }
        }
Пример #3
0
        private bool disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    if (fileWatcher != null)
                    {
                        fileWatcher.Changed -= OnChanged;
                        fileWatcher.Created -= OnChanged;
                        fileWatcher.Deleted -= OnChanged;
                        fileWatcher.Renamed -= OnRenamed;
                        fileWatcher          = null;
                    }
                }
                disposedValue = true;
            }
        }
Пример #4
0
        private FileSystemSafeWatcher CreateFileWatcher(string path)
        {
            // Create a new FileSystemSafeWatcher and set its properties.
            var watcher = new FileSystemSafeWatcher();

            watcher.Path = Path.GetDirectoryName(path);

            /* Watch for changes in LastAccess and LastWrite times, and
             * the renaming of files or directories. */
            watcher.NotifyFilter = NotifyFilters.LastWrite;
            watcher.Filter       = Path.GetFileName(path);

            // Add event handlers.
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);

            // Begin watching.
            watcher.EnableRaisingEvents = true;
            Helpers.Logging.TraceMessage($"FileSystemSafeWatcher: Watching {watcher.Path}\\{watcher.Filter} for changes.");
            return(watcher);
        }
Пример #5
0
#pragma warning restore IDE0052 // Remove unread private members

        public FileWatcher(string file)
        {
            fileWatcher = CreateFileWatcher(file);
        }
Пример #6
0
#pragma warning restore IDE0052 // Remove unread private members

        public CommandFileWatcher(string file)
        {
            fileWatcher = CreateFileWatcher(file);
        }
Пример #7
0
 public BufferingFileSystemWatcher(string path, string filter)
 {
     _containedFSW = new FileSystemSafeWatcher(path, filter);
 }
Пример #8
0
 public BufferingFileSystemWatcher(string path)
 {
     _containedFSW = new FileSystemSafeWatcher(path, "*.*");
 }
Пример #9
0
 public BufferingFileSystemWatcher()
 {
     _containedFSW = new FileSystemSafeWatcher();
 }
Пример #10
0
        /// <summary>
        /// 初始化监听器
        /// </summary>
        public void Initialize()
        {
            watcher = new FileSystemSafeWatcher(DirectoryToMonitor);

            watcher.NotifyFilter          = NotifyFilters.LastWrite;
            watcher.IncludeSubdirectories = !CurrentLevelOnly;

            watcher.Changed += (sender, e) =>
            {
                Logger.Instance.WriteInfoLog(String.Format("检测到文件夹修改. Type = {0}, Dir = {1}, FullPath = {2}", e.ChangeType.ToString(), DirectoryToMonitor, e.FullPath));

                for (byte i = 0; i < Behaviors.Length; i++)
                {
                    var behavior = Behaviors[i];

                    if (!behavior.Enable)
                    {
                        continue;
                    }

                    Dictionary <String, String> copiedArgCollection = new Dictionary <string, string>();

                    foreach (var pair in behavior.Parameters)
                    {
                        copiedArgCollection[pair.Key] = behavior.Parameters[pair.Key]
                                                        .Replace("$TARGET_FILE$", e.FullPath)
                                                        .Replace("&lt;", "<")
                                                        .Replace("&gt;", ">")
                                                        .Replace("&quot;", "\"")
                                                        .Replace("&nbsp;", " ");
                    }

                    try
                    {
                        if (behavior.IsAsync)
                        {
                            IAsyncBehavior s = (IAsyncBehavior)behavior.Behavior;

                            s.BeginDoAction(DirectoryToMonitor, copiedArgCollection, e, AsyncCallBack);

                            Logger.Instance.WriteInfoLog(String.Format("异步执行 {0}", behavior.Behavior));
                        }
                        else
                        {
                            var rs = behavior.Behavior.DoAction(DirectoryToMonitor, copiedArgCollection, e);

                            Logger.Instance.WriteInfoLog(String.Format("执行动作 {0} 的结果: {1}", behavior.Behavior, rs.ToString()));
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Instance.WriteErrorLog(String.Format("执行针对 {0} 的 {1} 执行响应动作 {2} 时发生错误: {3}.\n调用堆栈:\n{4}",
                                                                    DirectoryToMonitor, e.ChangeType.ToString(), behavior.Behavior, ex.Message, ex.StackTrace));

                        continue;
                    }
                }
            };

            watcher.EnableRaisingEvents = true;
        }