zoukankan      html  css  js  c++  java
  • 使用FileSystemWatcher监视文件变化

    本文转载:http://www.cnblogs.com/zanxiaofeng/archive/2011/01/08/1930583.html

    FileSystemWatcher基础

    属性:

        Path——这个属性告诉FileSystemWatcher它需要监控哪条路径。例如,如果我们将这个属性设为“C: est”,对象就监控test目录下所有文件发生的所有改变(包括删除,修改,创建,重命名)。

        IncludeSubDirectories——这个属性说明FileSystemWatcher对象是否应该监控子目录中(所有文件)发生的改变。

        Filter——这个属性允许你过滤掉某些类型的文件发生的变化。例如,如果我们只希望在TXT文件被修改/新建/删除时提交通知,可以将这个属性设为“*txt”。在处理高流量或大型目录时,使用这个属性非常方便。

    NotifyFilter——获取或设置要监视的更改类型。可以进一步的过滤要监控的更改类型,如watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite

               | NotifyFilters.FileName | NotifyFilters.DirectoryName;

    事件:

        Changed——当被监控的目录中有一个文件被修改时,就提交这个事件。值得注意的是,这个事件可能会被提交多次,即使文件的内容仅仅发生一项改变。这是由于在保存文件时,文件的其它属性也发生了改变。

        Created——当被监控的目录新建一个文件时,就提交这个事件。如果你计划用这个事件移动新建的事件,你必须在事件处理器中写入一些错误处理代码,它能处理当前文件被其它进程使用的情况。之所以要这样做,是因为Created事件可能在建立文件的进程释放文件之前就被提交。如果你没有准备正确处理这种情况的代码,就可能出现异常。

        Deleted——当被监控的目录中有一个文件被删除,就提交这个事件。

        Renamed——当被监控的目录中有一个文件被重命名,就提交这个事件。 

    注:如果你没有将EnableRaisingEvents设为真,系统不会提交任何一个事件。如果有时FileSystemWatcher对象似乎无法工作,请首先检查EnableRaisingEvents,确保它被设为真。

    问题:

      程序里需要监视某个目录下的文件变化情况: 一旦目录中出现新文件或者旧的文件被覆盖,程序需要读取文件内容并进行处理;但在实际处理中发现当一个文件产生变化时,Change事件被反复触发了好几次。这样可能的结果是造成同一文件的重复处理。
    解决方法:

      针对上面的问题,于是写了一个可以延迟FileSystemWatcher发出的事件的Class DelayFileSystemWatcher。

    复制代码
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Threading;

    namespace Utility
    {
    public class DelayFileSystemWatcher
    {
    private readonly Timer m_Timer;
    private readonly Int32 m_TimerInterval;
    private readonly FileSystemWatcher m_FileSystemWatcher;
    private readonly FileSystemEventHandler m_FileSystemEventHandler;
    private readonly Dictionary<String, FileSystemEventArgs> m_ChangedFiles = new Dictionary<string, FileSystemEventArgs>();

    public DelayFileSystemWatcher(string path, string filter, FileSystemEventHandler watchHandler, int timerInterval)
    {
    m_Timer = new Timer(OnTimer, null, Timeout.Infinite, Timeout.Infinite);
    m_FileSystemWatcher = new FileSystemWatcher(path, filter);
    m_FileSystemWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.CreationTime;
    m_FileSystemWatcher.Created += fileSystemWatcher_Changed;
    m_FileSystemWatcher.Changed += fileSystemWatcher_Changed;
    m_FileSystemWatcher.Deleted += fileSystemWatcher_Changed;
    m_FileSystemWatcher.Renamed += fileSystemWatcher_Changed;
    m_FileSystemWatcher.EnableRaisingEvents = true;
    m_FileSystemEventHandler = watchHandler;
    m_TimerInterval = timerInterval;
    }

    public void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
    {
    lock (m_ChangedFiles)
    {
    if (!m_ChangedFiles.ContainsKey(e.Name))
    {
    m_ChangedFiles.Add(e.Name, e);
    }
    }
    m_Timer.Change(m_TimerInterval, Timeout.Infinite);
    }

    private void OnTimer(object state)
    {
    Dictionary<String, FileSystemEventArgs> tempChangedFiles = new Dictionary<String, FileSystemEventArgs>();

    lock (m_ChangedFiles)
    {
    foreach (KeyValuePair<string, FileSystemEventArgs> changedFile in m_ChangedFiles)
    {
    tempChangedFiles.Add(changedFile.Key, changedFile.Value);
    }
    m_ChangedFiles.Clear();
    }

    foreach (KeyValuePair<string, FileSystemEventArgs> changedFile in tempChangedFiles)
    {
    m_FileSystemEventHandler(this, changedFile.Value);
    }
    }
    }
    }
    复制代码

    使用方式如下:

    复制代码
            s_DelayFileSystemWatcher = new DelayFileSystemWatcher(@"C:Temp", "*.xml", fileSystemWatcher_Changed, 1500);

    private static void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
    {
    switch (e.ChangeType)
    {
    case WatcherChangeTypes.Created:
    //TODO
    break;
    case WatcherChangeTypes.Deleted:
    //TODO
    break;
    case WatcherChangeTypes.Changed:
    //TODO
    break;
    default:
    break;
    }
    }
    复制代码
  • 相关阅读:
    研究性能测试工具之systemtap入门之latencytap脚本应用(一)
    研究性能测试工具之systemtap入门指南(三)
    内存泄露bug指南(二)
    研究性能测试工具之systemtap入门之latencytap脚本应用(二)
    内存泄露bug指南(一)
    VS2005 中文版下载
    Nhibernate事务出错
    页面生存周期
    web services enhancement (wse) 2.0/3.0研究与应用
    C#实现仿msn提示消息在登录后只弹出一次的效果(downmoon)
  • 原文地址:https://www.cnblogs.com/51net/p/3813250.html
Copyright © 2011-2022 走看看