文件夹监视

1. 有一个专门的类用于监视文件夹变动 FileSystemWatcher

    设置该对象相关属性。 注:不能同事监视多种扩展名的文件对象,如果需要监视多种类型的文件的话需要定义多个对象分别实现

            _ttfWatcher.Path = titleFolderSelection.Path;
            _ttfWatcher.Filter = "*.ttf";
            _ttfWatcher.Changed += OnProcess;
            _ttfWatcher.Created += OnProcess;
            _ttfWatcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess
                                   | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
            _ttfWatcher.IncludeSubdirectories = true;

 

2. 定义文件发生变动时调用什么方法

        private static void OnProcess(object source, FileSystemEventArgs e)
        {
            if (e.ChangeType == WatcherChangeTypes.Created)
            {
                // 处理
            }
            else if (e.ChangeType == WatcherChangeTypes.Changed)
            {
                // 处理
            }
        }

 

3. 利用EnableRaisingEvents来设置是否触发之前定义的方法

posted @ 2014-04-04 11:20  Alf7  阅读(242)  评论(0)    收藏  举报