Custom Search

Sunday, September 30, 2007

Using File System Watcher in Vb.Net

Imports System
Imports System.IO

Class FileWatch

Shared Sub main()
Dim file_watch As New FileSystemWatcher()

' Path to monitor
file_watch.Path = "c:\"

' Uncomment to watch files
' file_watch.Target = IO.WatcherTarget.File

file_watch.IncludeSubdirectories = True

' Additional filtering
file_watch.Filter = "*.*"
file_watch.EnableRaisingEvents = true

'Add the event handler for creation of new files only
AddHandler file_watch.created, New FileSystemEventHandler(AddressOf OnFileEvent)

' file_watch.Enabled = True

' Dont Exit
console.readline()
End Sub

' Event that will be raised when a new file is created
Shared Sub OnFileEvent(ByVal source As Object, ByVal e As FileSystemEventArgs)
console.writeline("New File Created in C: ")
End Sub

End Class

No comments: