I try to monitor a directory for new created files that in this example
shall be moved to another dir. So far it looks like working as it should but
after the first file (one file copied into the dir) it detect a new file
created and go trough the routine again.
I have turned off the IncludeSubdirectories but it still do it
The code is the following
Imports System.IO
Public Class frmFileWatcher
Inherits System.Windows.Forms.Form
Private WithEvents fsWatchObj As FileSystemWatcher
Dim DispMessage As Boolean
Private Sub SetupWatcher(ByVal PathToWatch As String)
fsWatchObj = New FileSystemWatcher(PathToWatch)
fsWatchObj.IncludeSubdirectories = False ' True
fsWatchObj.EnableRaisingEvents = True
fsWatchObj.NotifyFilter = fsWatchObj.NotifyFilter Or _
IO.NotifyFilters.FileName
fsWatchObj.NotifyFilter = fsWatchObj.NotifyFilter Or _
IO.NotifyFilters.LastWrite
AddHandler fsWatchObj.Created, New FileSystemEventHandler(AddressOf
Me.FileCreated)
End Sub
Private Sub FileCreated(ByVal s As System.Object, ByVal EventInfo As
FileSystemEventArgs)
Dim DisplayMessage As String
If (EventInfo.ChangeType = WatcherChangeTypes.Created) Then
DisplayMessage = "File " & EventInfo.Name & " was "
DisplayMessage &= EventInfo.ChangeType.ToString & " at " &
DateTime.Now.ToString
MsgBox(DisplayMessage)
lstFiles.Items.Add(DisplayMessage)
FileCopy(EventInfo.FullPath, "e:\resizer\ut\" & EventInfo.Name)
End If
End Sub
Private Sub FileRenamed(ByVal s As System.Object, ByVal EventInfo As
RenamedEventArgs)
Dim DisplayMessage As String
DisplayMessage = "File " & EventInfo.OldName
DisplayMessage &= " was renamed " & EventInfo.Name & " at " &
DateTime.Now.ToString
lstFiles.Items.Add(DisplayMessage)
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStart.Click
If txtDirToWatch.Text = "" Then
MessageBox.Show("Please enter path to watch", "Enter Path",
MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
Call SetupWatcher(txtDirToWatch.Text)
End If
End Sub
End Class
Petter L.