FileSystemWatcher help in VB2005 Express

nbrege

Active member
Joined
Jan 12, 2006
Messages
34
I am writing a program that watches a certain folder for incoming files & then moves those files to another folder. Heres the problem: if I copy a group of files into the watched folder it works fine, but if I move a group of files into the watched folder it doesnt do anything, the event handler never triggers. What is the proper notify filter to use to watch for new files in a folder? I have tried both the Size & LastWrite filters but neither does what I want. Any help would be appreciated. Here is the code I have so far:


[VB]
Imports System.IO
Imports System.Diagnostics

Public Class Form1

Const watchfolder As String = "c:\checked"
Const targetfolder As String = "c:\checked2"
Dim fsw As New FileSystemWatcher(watchfolder)

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Watch for changes in folder size
fsw.NotifyFilter = (NotifyFilters.LastWrite)

Register a handler that gets called when a file is created, changed, or deleted.
AddHandler fsw.Changed, New FileSystemEventHandler(AddressOf OnChanged)

Begin watching.
fsw.EnableRaisingEvents = True

End Sub

Private Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)

folder contents have changed

Console.Beep()

Create a reference to the current directory.
Dim di As New DirectoryInfo(watchfolder)
Create an array representing the files in the current directory.
Dim fi As FileInfo() = di.GetFiles()
Console.WriteLine("The following files exist in the current directory:")
Print out the names of the files in the current directory.
Dim fiTemp As FileInfo
For Each fiTemp In fi
Console.WriteLine(fiTemp.Name)
My.Computer.FileSystem.MoveFile(watchfolder & "\" & fiTemp.Name, targetfolder & "\" & fiTemp.Name, True)
Next fiTemp


End Sub
[/VB]


Thanks...
 
Back
Top