1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
Imports System.IO
Sub Watch(ByVal sPath As String, Optional ByVal sFilter As String = "*")
'On peut utiliser les '*' avec sFilter Dim fw As New FileSystemWatcher(sPath, sFilter)
'On ajoute les handler pour surveiller les évènements qu'on souhaite.
AddHandler fw.Changed, AddressOf OnChanged
AddHandler fw.Renamed, AddressOf OnRenamed
AddHandler fw.Created, AddressOf OnChanged
AddHandler fw.Deleted, AddressOf OnChanged
AddHandler fw.Error, AddressOf OnError
'On surveillera aussi les sous répertoires
fw.IncludeSubdirectories = True
' Mettre cette propriété à True démarre la surveillance des modifications. La mettre à false l'arrête.
Dans notre exemple, elle sera à true durant tout le programme fw.EnableRaisingEvents = True
End Sub
Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
Console.WriteLine("File {0} {1}", e.FullPath, e.ChangeType)
End Sub
Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)
Console.WriteLine("File {0} renamed to {1}", e.OldFullPath, e.FullPath)
End Sub
Sub OnError(ByVal source As Object, ByVal e As ErrorEventArgs) Dim ex As Exception = e.GetException()
Debug.WriteLine(ex.ToString()) Console.WriteLine(ex.Message)
End Sub |
Partager