一个封装好的 FileSystemWatcher 类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Collections;
  7. using System.Threading;
  8. namespace synchro
  9. {
  10.     public delegate void Completed(string key);
  11.     public class MyFileSystemWather
  12.     {
  13.         private FileSystemWatcher fsWather;
  14.         private Hashtable hstbWather;
  15.         public event RenamedEventHandler OnRenamed;
  16.         public event FileSystemEventHandler OnChanged;
  17.         /// <summary>
  18.         /// 构造函数
  19.         /// </summary>
  20.         /// <param name="path">要监控的路径</param>
  21.         public MyFileSystemWather(string path)
  22.         {
  23.             if (!Directory.Exists(path)) throw new Exception("找不到路径:" + path);
  24.             fsWather = new FileSystemWatcher(path);
  25.             fsWather.IncludeSubdirectories = true;
  26.             fsWather.Filter = "*.*";
  27.             fsWather.Changed += new FileSystemEventHandler(fsWather_Changed);
  28.             fsWather.Created += new FileSystemEventHandler(fsWather_Changed);
  29.             fsWather.Deleted += new FileSystemEventHandler(fsWather_Changed);
  30.             fsWather.Renamed += new RenamedEventHandler(fsWather_Renamed);
  31.             hstbWather = new Hashtable();
  32.         }
  33.         /// <summary>
  34.         /// 开始监控
  35.         /// </summary>
  36.         public void start()
  37.         {
  38.             fsWather.EnableRaisingEvents = true;
  39.         }
  40.         /// <summary>
  41.         /// 停止监控
  42.         /// </summary>
  43.         public void stop()
  44.         {
  45.             fsWather.EnableRaisingEvents = false;
  46.         }
  47.         /// <summary>
  48.         /// filesystemWatcher 本身的事件通知处理过程
  49.         /// </summary>
  50.         /// <param name="sender"></param>
  51.         /// <param name="e"></param>
  52.         private void fsWather_Renamed(object sender, RenamedEventArgs e)
  53.         {
  54.             lock (hstbWather) { hstbWather.Add(e.FullPath, e); }
  55.             watcherDeal wd = new watcherDeal(sender, e);
  56.             wd.OnCompleted += new Completed(wd_OnCompleted);
  57.             wd.OnRenamed += new RenamedEventHandler(wd_OnRenamed);
  58.             Thread tdDeal = new Thread(wd.dealWith);
  59.             tdDeal.Start();
  60.         }
  61.         void wd_OnRenamed(object sender, RenamedEventArgs e)
  62.         {
  63.             OnRenamed(sender, e);
  64.         }
  65.         private void fsWather_Changed(object sender, FileSystemEventArgs e)
  66.         {
  67.             //File.AppendAllText(@"e:/111.txt",DateTime.Now.ToShortTimeString() + ":" + e.FullPath + "/r/n");
  68.             if (e.ChangeType == WatcherChangeTypes.Changed)
  69.             {
  70.                 if (isFileUsed(e.FullPath)) return;
  71.                 if (hstbWather.ContainsKey(e.FullPath))
  72.                 {
  73.                     WatcherChangeTypes oldType = ((FileSystemEventArgs)hstbWather[e.FullPath]).ChangeType;
  74.                     if (oldType == WatcherChangeTypes.Created || oldType == WatcherChangeTypes.Changed) return;
  75.                 }
  76.             }
  77.             while (hstbWather.ContainsKey(e.FullPath)) { }
  78.             lock (hstbWather) { hstbWather.Add(e.FullPath, e); }
  79.             watcherDeal wd = new watcherDeal(sender, e);
  80.             wd.OnCompleted += new Completed(wd_OnCompleted);
  81.             wd.OnChanged += new FileSystemEventHandler(wd_OnChanged);
  82.             Thread tdDeal = new Thread(wd.dealWith);
  83.             tdDeal.Start();
  84.         }
  85.         void wd_OnChanged(object sender, FileSystemEventArgs e)
  86.         {
  87.             OnChanged(sender, e);
  88.         }
  89.         void wd_OnCompleted(string key)
  90.         {
  91.             lock (hstbWather)
  92.             {
  93.                 hstbWather.Remove(key);
  94.             }
  95.         }
  96.         private static bool isFileUsed(string fileName)
  97.         {
  98.             try
  99.             {
  100.                 FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
  101.                 fs.Close();
  102.                 return false;
  103.             }
  104.             catch (Exception)
  105.             {
  106.                 if (!File.Exists(fileName))
  107.                     return false;
  108.                 else
  109.                     return true;
  110.             }
  111.         }
  112.         private class watcherDeal
  113.         {
  114.             private object sender;
  115.             private object eParam;
  116.             public event RenamedEventHandler OnRenamed;
  117.             public event FileSystemEventHandler OnChanged;
  118.             public event Completed OnCompleted;
  119.             public watcherDeal(object sender, object eParam)
  120.             {
  121.                 this.sender = sender;
  122.                 this.eParam = eParam;
  123.             }
  124.             public void dealWith()
  125.             {
  126.                 if (eParam.GetType() == typeof(RenamedEventArgs))
  127.                 {
  128.                     OnRenamed(sender, (RenamedEventArgs)eParam);
  129.                     OnCompleted(((RenamedEventArgs)eParam).FullPath);
  130.                 }
  131.                 else
  132.                 {
  133.                     FileSystemEventArgs e = (FileSystemEventArgs)eParam;
  134.                     if (e.ChangeType == WatcherChangeTypes.Created)
  135.                     {
  136.                         while (isFileUsed(e.FullPath)) { }
  137.                     }
  138.                     OnChanged(sender, e);
  139.                     OnCompleted(e.FullPath);
  140.                 }
  141.             }
  142.         }
  143.     }
  144. }

可以把 Filter , IncludeSubdirectories 等属性加上。

此类可以排除一个动作引发多个事件的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值