1、支持对目标目录下的文件进行监控,监控动作又修改、删除、创建
2、支持对目录下文件的过滤,比如只针对某一个文件进行监控
3、修改了原来例子中输出监控日志,增加了一个执行启动应用的功能,比如监控到某一个文件变动,可以执行另外一个程序
可以用来实现对某类写临时文件的程序,进行中间数据截取
关键代码和上次一个贴友发的一样,只是这个是winfom的
[C#] 纯文本查看 复制代码 private void startActivityMonitoring(string sPath)
{
if (sPath.Length < 3)
{
MessageBox.Show("You have to enter a folder to monitor.",
"Hey..!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
abortAcitivityMonitoring();
}
else
{
// This is the path we want to monitor
_watchFolder.Path = sPath;
// Make sure you use the OR on each Filter because we need to monitor
// all of those activities
_watchFolder.NotifyFilter = System.IO.NotifyFilters.DirectoryName;
_watchFolder.NotifyFilter = _watchFolder.NotifyFilter | System.IO.NotifyFilters.FileName;
_watchFolder.NotifyFilter = _watchFolder.NotifyFilter | System.IO.NotifyFilters.Attributes;
// Now hook the triggers(events) to our handler (eventRaised)
_watchFolder.Changed += new FileSystemEventHandler(eventRaised);
_watchFolder.Created += new FileSystemEventHandler(eventRaised);
_watchFolder.Deleted += new FileSystemEventHandler(eventRaised);
// Occurs when a file or directory is renamed in the specific path
_watchFolder.Renamed += new System.IO.RenamedEventHandler(eventRenameRaised);
// And at last.. We connect our EventHandles to the system API (that is all
// wrapped up in System.IO)
try
{
_watchFolder.EnableRaisingEvents = true;
}
catch (ArgumentException)
{
abortAcitivityMonitoring();
}
}
}
|