Timer用法(监控目录结构变动)
using System;
using System.IO ;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using log4net;
namespace DirectoryMonitor
{
/// <summary>
/// 监控文件目录变化
/// 使用<see cref="FileSystemWatcher"/>进行文件监控,可能同时产生多个事件
/// 采用一个定时器可以将多个事件提示转换成一个单一事件,定时器需要等待
/// <see cref="TimeoutMillis"/>后才会触发下一个事件提示
/// </summary>
public sealed class MonitorFS
{
#region "Private Method"
private static readonly ILog Log = LogManager.GetLogger(typeof(MonitorFS));
private Timer m_timer;
private const int TimeoutMillis = 1000;
private string strPath;
private string strCmd;
#endregion
/// <summary>
/// 启动监控
/// </summary>
/// <param name="path"></param>
/// <param name="cmd"></param>
internal static void StartWatching(string path, string cmd)
{
new MonitorFS(path, cmd);
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="path">要监视的文件夹</param>
/// <param name="cmd">要执行的命令</param>
private MonitorFS(string path, string cmd)
{
strPath = path;
strCmd = cmd;
FileSystemWatcher watcher = new FileSystemWatcher(path);
// 注册文件及文件夹创建、删除、重命名事件
watcher.Created += new FileSystemEventHandler(watcher_Created);
watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
watcher.Filter = "*.*";
watcher.IncludeSubdirectories = true;
//开始监视
watcher.EnableRaisingEvents = true;
m_timer = new Timer(new TimerCallback(OnWatchedChange), null, Timeout.Infinite, Timeout.Infinite);
}
/// <summary>
/// 文件或路径创建
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void watcher_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine(DateTime .Now .ToString () + " File Created");
m_timer.Change(TimeoutMillis, Timeout.Infinite);
}
/// <summary>
/// 文件或文件路径删除时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void watcher_Deleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine(DateTime.Now.ToString() + " File Delete");
m_timer.Change(TimeoutMillis, Timeout.Infinite);
}
/// <summary>
/// 文件或文件路径改名时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void watcher_Renamed(object sender, FileSystemEventArgs e)
{
Console.WriteLine(DateTime.Now.ToString() + " File Change");
m_timer.Change(TimeoutMillis, Timeout.Infinite);
}
/// <summary>
///
/// </summary>
/// <param name="state"></param>
private void OnWatchedChange(object state)
{
Execute();
}
/// <summary>
///
/// </summary>
private void Execute()
{
if (string.IsNullOrEmpty (strCmd ))
return ;
try
{
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = strCmd;
startinfo.UseShellExecute = false;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardOutput = true;
Process myProcess = new Process();
myProcess.StartInfo = startinfo;
myProcess.Start();
StreamReader sr = myProcess.StandardOutput;
string info = sr.ReadToEnd();
Log.Info(info);
}
catch (Exception ex)
{
Log.Error("Execute " + strCmd,ex );
}
finally
{
Log.Info("\r\n==========================================\r\n");
}
}
}
}