用FileSystemWatcher监控作业。

.NET Framework中的System.IO命名空间包括FileSystemWatcher类。这个类提供监控作业的目录或文件的功能。如果你的应用程序需要知道新文件是何时被加入指定目录的或者文件是何时被删除的,那么这个功能会很有用处。

  要使用FileSystemWatcher,首先要创建一个类的实例。


  Private dirWatcher As New system.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属性后,添加事件处理器来捕获事件,并确定它能够激发事件。


  AddHandler dirWatcher.Created, AddressOf Me.OnCreation
  AddHandler dirWatcher.Changed, AddressOf Me.OnCreation
  dirWatcher.EnableRaisingEvents = True


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


  Public Shared Sub OnCreation(ByVal source As Object, _
  ByVale As system.IO.FileSystemEventArgs)
  Debug.WriteLine("file: " & e.FullPath
  & " " & e.ChangeType)
  End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值