コード例 #1
0
        private FileSystemWatcher BuildFileWatcher()
        {
            FileWatcherTimer watcherTimer = new FileWatcherTimer(WatchChangedHandler);
            // 创建 FileSystemWatcher 及相关属性.
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = this.DirectoryPath;
            //监视时所包含的文件名
            watcher.Filter = this.Filter;
            watcher.IncludeSubdirectories = true;

            //过滤条件
            watcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            if (this.ActionTypes.Contains(WatcherChangeTypes.Changed))
            {
                watcher.Changed += new FileSystemEventHandler(watcherTimer.OnFileChangedCallback);
            }

            if (this.ActionTypes.Contains(WatcherChangeTypes.Created))
            {
                watcher.Created += new FileSystemEventHandler(watcherTimer.OnFileChangedCallback);
            }

            if (this.ActionTypes.Contains(WatcherChangeTypes.Deleted))
            {
                watcher.Deleted += new FileSystemEventHandler(watcherTimer.OnFileChangedCallback);
            }

            if (this.ActionTypes.Contains(WatcherChangeTypes.Renamed))
            {
                watcher.Renamed += new RenamedEventHandler(watcherTimer.OnFileChangedCallback);
            }

            // 启动监视
            watcher.EnableRaisingEvents = true;

            return watcher;
        }
コード例 #2
0
ファイル: FileMonitor.cs プロジェクト: uwitec/higirl360-taoke
        private FileSystemWatcher BuildFileWatcher()
        {
            FileWatcherTimer watcherTimer = new FileWatcherTimer(WatchChangedHandler);
            // 创建 FileSystemWatcher 及相关属性.
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = this.DirectoryPath;
            //监视时所包含的文件名
            watcher.Filter = this.Filter;
            watcher.IncludeSubdirectories = true;

            //过滤条件
            watcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            if (this.ActionTypes.Contains(WatcherChangeTypes.Changed))
            {
                watcher.Changed += new FileSystemEventHandler(watcherTimer.OnFileChangedCallback);
            }

            if (this.ActionTypes.Contains(WatcherChangeTypes.Created))
            {
                watcher.Created += new FileSystemEventHandler(watcherTimer.OnFileChangedCallback);
            }

            if (this.ActionTypes.Contains(WatcherChangeTypes.Deleted))
            {
                watcher.Deleted += new FileSystemEventHandler(watcherTimer.OnFileChangedCallback);
            }

            if (this.ActionTypes.Contains(WatcherChangeTypes.Renamed))
            {
                watcher.Renamed += new RenamedEventHandler(watcherTimer.OnFileChangedCallback);
            }

            // 启动监视
            watcher.EnableRaisingEvents = true;

            return watcher;
        }