首页 > 学院 > 开发设计 > 正文

用FileSystemWatcher监控作业

2019-11-18 17:38:53
字体:
来源:转载
供稿:网友
要使用FileSystemWatcher,首先要创建一个类的实例。

PRivatedirWatcherAsNewSystem.IO.FileSystemWatcher()

  接下来,通过设置Path属性设置FileSystemWatcher来监控指定目录。可以设置IncludeSubdirectories属性监控指定目录下的所有子目录。

dirWatcher.Path="C:/Temp"
dirWatcher.IncludeSubdirectories=False

  Filter属性指定目录内要监控的文件。这个属性接受通配符,所以所有的文本文件都可以通过将它设定为"*.txt"文件来监控。指定特殊文件名后只会对那个文件起作用。

dirWatcher.Filter="*.txt"

  NotifyFilter属性决定被监控的指定文件的属性。

dirWatcher.NotifyFilter=System.IO.NotifyFilters.Lastaccess
Or_
System.IO.NotifyFilters.LastWrite

  在设定FileSystemWatcher属性后,添加事件处理器来捕获事件,并确定它能够激发事件。

AddHandlerdirWatcher.Created,AddressOfMe.OnCreation
AddHandlerdirWatcher.Changed,AddressOfMe.OnCreation
dirWatcher.EnableRaisingEvents=True

  最后,添加一个新的子程序来处理事件。

PublicSharedSubOnCreation(ByValsourceAsObject,_
ByValeAsSystem.IO.FileSystemEventArgs)
Debug.WriteLine("File:"&e.FullPath
&""&e.ChangeType)
EndSub

->


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表