Monitor de Archivos
System.IO.FileSystemWatcher()
Fecha:
02/May/04 (01/05/04)
Autor: Ariel G. |[email protected]|
El siguiente código utiliza el espacio de nombres System.IO.SyistemWatcher para generar un evento si se producen cambios en los archivos contenidos en el directorio indicado, cuando se produce un cambio (como eliminar un archivo, renombrarlo, crear uno nuevo, o abrirlo), se dispara un evento.
Código en VB.net:
Imports System.IO Imports Microsoft.VisualBasic.FileSystem Public Class Form1 Inherits System.Windows.Forms.Form � #Region " C�digo generado por el Dise�ador de Windows Forms " '...� #End Region � Private Sub Wachter() Dim MyWatcher As New System.IO.FileSystemWatcher() ' crea un nuevo objeto System.IO.FileSystemWatcher() MyWatcher.Path = txtPath.Text ' directorio a monitorear MyWatcher.IncludeSubdirectories = True ' indica si se deben monitorear los subdirectorios '======EVENTOS AddHandler MyWatcher.Changed, AddressOf OnChanged AddHandler MyWatcher.Created, AddressOf OnCreated AddHandler MyWatcher.Deleted, AddressOf OnDeleted AddHandler MyWatcher.Renamed, AddressOf OnRenamed MyWatcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName) MyWatcher.EnableRaisingEvents = True End Sub � Public WChanged As Integer '1 Public WCreated As Integer '2 Public WRenamed As Integer '3 Public WDeleted As Integer '4 � Public Sub SetStatics(ByVal val As Integer) Select Case val Case 1 WChanged = WChanged + 1 Case 2 WCreated = WCreated + 1 Case 3 WRenamed = WRenamed + 1 Case 4 WDeleted = WDeleted + 1 End Select lblChanged.Text = WChanged lblDeleted.Text = WDeleted lblCreated.Text = WCreated lblRenamed.Text = WRenamed End Sub � Public Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs) ' se dispara cuando se modifica o accede a un archivo SetStatics(1) End Sub Public Sub OnDeleted(ByVal source As Object, ByVal e As FileSystemEventArgs) ' al eliminar un archivo SetStatics(4) End Sub Public Sub OnCreated(ByVal source As Object, ByVal e As FileSystemEventArgs) ' al crear un archivo SetStatics(2) End Sub Public Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs) ' renombrar un archivo SetStatics(3) End Sub �� Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Wachter() End Sub End Class
Espero que les sea útil.«•...::HAG::...•»
Fichero con el código de ejemplo: HAG_MonitorDeArchivos.zip - 8.47 KB