63 lines
1.9 KiB
C#
63 lines
1.9 KiB
C#
|
|
using System;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Threading;
|
|||
|
|
|
|||
|
|
namespace CYQ.Data.Tool
|
|||
|
|
{
|
|||
|
|
internal class IOWatch
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>б<EFBFBD><D0B1><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
//private static MList<string> watchPathList = new MList<string>();
|
|||
|
|
private static MDictionary<string, IOWatch> watchs = new MDictionary<string, IOWatch>();
|
|||
|
|
private IOWatch()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
public static void On(string fileName, WatchDelegate watch)
|
|||
|
|
{
|
|||
|
|
if (!watchs.ContainsKey(fileName))
|
|||
|
|
{
|
|||
|
|
IOWatch fileWatch = new IOWatch();
|
|||
|
|
watchs.Add(fileName, fileWatch);
|
|||
|
|
fileWatch.WatchOn(fileName, watch);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public delegate void WatchDelegate(FileSystemEventArgs e);
|
|||
|
|
private WatchDelegate watch;
|
|||
|
|
private FileSystemWatcher fsy;
|
|||
|
|
public void WatchOn(string fileName, WatchDelegate watch)
|
|||
|
|
{
|
|||
|
|
this.watch = watch;
|
|||
|
|
this.fsy = new FileSystemWatcher(Path.GetDirectoryName(fileName), Path.GetFileName(fileName));
|
|||
|
|
fsy.EnableRaisingEvents = true;
|
|||
|
|
fsy.IncludeSubdirectories = false;
|
|||
|
|
fsy.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;
|
|||
|
|
fsy.Changed += new FileSystemEventHandler(fsy_Changed);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
private DateTime lastTime = DateTime.MinValue;
|
|||
|
|
private void fsy_Changed(object sender, FileSystemEventArgs e)
|
|||
|
|
{
|
|||
|
|
if (lastTime.AddSeconds(3) > DateTime.Now)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8>¼<EFBFBD>
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
//Log.WriteLogToTxt("IOWatch.On Change :" + e.FullPath, LogType.Debug);
|
|||
|
|
lastTime = DateTime.Now;
|
|||
|
|
lock (e.FullPath)
|
|||
|
|
{
|
|||
|
|
if (watch != null)
|
|||
|
|
{
|
|||
|
|
Thread.Sleep(10);//<2F><>ʱ<EFBFBD><CAB1><EFBFBD>ȴ<EFBFBD><C8B4>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>꣬<EFBFBD><EAA3AC><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><D0BF>ܶ<EFBFBD><DCB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
|||
|
|
watch(e);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|