- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Collections;
- using System.Threading;
- namespace synchro
- {
- public delegate void Completed(string key);
- public class MyFileSystemWather
- {
- private FileSystemWatcher fsWather;
- private Hashtable hstbWather;
- public event RenamedEventHandler OnRenamed;
- public event FileSystemEventHandler OnChanged;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="path">要监控的路径</param>
- public MyFileSystemWather(string path)
- {
- if (!Directory.Exists(path)) throw new Exception("找不到路径:" + path);
- fsWather = new FileSystemWatcher(path);
- fsWather.IncludeSubdirectories = true;
- fsWather.Filter = "*.*";
- fsWather.Changed += new FileSystemEventHandler(fsWather_Changed);
- fsWather.Created += new FileSystemEventHandler(fsWather_Changed);
- fsWather.Deleted += new FileSystemEventHandler(fsWather_Changed);
- fsWather.Renamed += new RenamedEventHandler(fsWather_Renamed);
- hstbWather = new Hashtable();
- }
- /// <summary>
- /// 开始监控
- /// </summary>
- public void start()
- {
- fsWather.EnableRaisingEvents = true;
- }
- /// <summary>
- /// 停止监控
- /// </summary>
- public void stop()
- {
- fsWather.EnableRaisingEvents = false;
- }
- /// <summary>
- /// filesystemWatcher 本身的事件通知处理过程
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void fsWather_Renamed(object sender, RenamedEventArgs e)
- {
- lock (hstbWather) { hstbWather.Add(e.FullPath, e); }
- watcherDeal wd = new watcherDeal(sender, e);
- wd.OnCompleted += new Completed(wd_OnCompleted);
- wd.OnRenamed += new RenamedEventHandler(wd_OnRenamed);
- Thread tdDeal = new Thread(wd.dealWith);
- tdDeal.Start();
- }
- void wd_OnRenamed(object sender, RenamedEventArgs e)
- {
- OnRenamed(sender, e);
- }
- private void fsWather_Changed(object sender, FileSystemEventArgs e)
- {
- //File.AppendAllText(@"e:/111.txt",DateTime.Now.ToShortTimeString() + ":" + e.FullPath + "/r/n");
- if (e.ChangeType == WatcherChangeTypes.Changed)
- {
- if (isFileUsed(e.FullPath)) return;
- if (hstbWather.ContainsKey(e.FullPath))
- {
- WatcherChangeTypes oldType = ((FileSystemEventArgs)hstbWather[e.FullPath]).ChangeType;
- if (oldType == WatcherChangeTypes.Created || oldType == WatcherChangeTypes.Changed) return;
- }
- }
- while (hstbWather.ContainsKey(e.FullPath)) { }
- lock (hstbWather) { hstbWather.Add(e.FullPath, e); }
- watcherDeal wd = new watcherDeal(sender, e);
- wd.OnCompleted += new Completed(wd_OnCompleted);
- wd.OnChanged += new FileSystemEventHandler(wd_OnChanged);
- Thread tdDeal = new Thread(wd.dealWith);
- tdDeal.Start();
- }
- void wd_OnChanged(object sender, FileSystemEventArgs e)
- {
- OnChanged(sender, e);
- }
- void wd_OnCompleted(string key)
- {
- lock (hstbWather)
- {
- hstbWather.Remove(key);
- }
- }
- private static bool isFileUsed(string fileName)
- {
- try
- {
- FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
- fs.Close();
- return false;
- }
- catch (Exception)
- {
- if (!File.Exists(fileName))
- return false;
- else
- return true;
- }
- }
- private class watcherDeal
- {
- private object sender;
- private object eParam;
- public event RenamedEventHandler OnRenamed;
- public event FileSystemEventHandler OnChanged;
- public event Completed OnCompleted;
- public watcherDeal(object sender, object eParam)
- {
- this.sender = sender;
- this.eParam = eParam;
- }
- public void dealWith()
- {
- if (eParam.GetType() == typeof(RenamedEventArgs))
- {
- OnRenamed(sender, (RenamedEventArgs)eParam);
- OnCompleted(((RenamedEventArgs)eParam).FullPath);
- }
- else
- {
- FileSystemEventArgs e = (FileSystemEventArgs)eParam;
- if (e.ChangeType == WatcherChangeTypes.Created)
- {
- while (isFileUsed(e.FullPath)) { }
- }
- OnChanged(sender, e);
- OnCompleted(e.FullPath);
- }
- }
- }
- }
- }
可以把 Filter , IncludeSubdirectories 等属性加上。
此类可以排除一个动作引发多个事件的问题。