aclayborne
Programmer
I'm using the FileSystemWatcher to sync 2 folders.
I'm receiving errors. because the changed event fires multiple times.
I'm good on file updates and deletions. It's the copies(a create even is kick off and then a changed event)
Any suggestions.
' Init
Sub Init
Dim FSWL As FileSystemWatcher = New FileSystemWatcher
FSWL.Path = StorageLocal
FSWL.Filter = "*.txt"
'FSWL.SynchronizingObject = Me
FSWL.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
AddHandler FSWL.Changed, AddressOf OnChanged
AddHandler FSWL.Created, AddressOf OnChanged
AddHandler FSWL.Deleted, AddressOf OnChanged
AddHandler FSWL.Renamed, AddressOf OnChanged
FSWL.EnableRaisingEvents = True
End Sub
Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)
If ProjectStorageCaching Then
Try
Select Case e.ChangeType
Case IO.WatcherChangeTypes.Changed
System.IO.File.Copy(StorageLocal & e.Name, StorageCache & e.Name, True)
Case IO.WatcherChangeTypes.Deleted
If File.Exists(StorageCache & e.Name) Then System.IO.File.Delete(ProjectStorageCache & e.Name)
End If
Case IO.WatcherChangeTypes.Created
If Not File.Exists(StorageCache & e.Name) Then
System.IO.File.Create(StorageCache & e.Name)
End If
End Select
Catch
MsgBox("Caching Error!")
End Try
End If
End Sub
Public Sub OnRename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)
If StorageCaching Then
If File.Exists(StorageCache & e.OldName) Then
System.IO.File.Copy(StorageCache & e.OldName, StorageCache & e.Name)
End If
End If
End Sub
I'm receiving errors. because the changed event fires multiple times.
I'm good on file updates and deletions. It's the copies(a create even is kick off and then a changed event)
Any suggestions.
' Init
Sub Init
Dim FSWL As FileSystemWatcher = New FileSystemWatcher
FSWL.Path = StorageLocal
FSWL.Filter = "*.txt"
'FSWL.SynchronizingObject = Me
FSWL.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
AddHandler FSWL.Changed, AddressOf OnChanged
AddHandler FSWL.Created, AddressOf OnChanged
AddHandler FSWL.Deleted, AddressOf OnChanged
AddHandler FSWL.Renamed, AddressOf OnChanged
FSWL.EnableRaisingEvents = True
End Sub
Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)
If ProjectStorageCaching Then
Try
Select Case e.ChangeType
Case IO.WatcherChangeTypes.Changed
System.IO.File.Copy(StorageLocal & e.Name, StorageCache & e.Name, True)
Case IO.WatcherChangeTypes.Deleted
If File.Exists(StorageCache & e.Name) Then System.IO.File.Delete(ProjectStorageCache & e.Name)
End If
Case IO.WatcherChangeTypes.Created
If Not File.Exists(StorageCache & e.Name) Then
System.IO.File.Create(StorageCache & e.Name)
End If
End Select
Catch
MsgBox("Caching Error!")
End Try
End If
End Sub
Public Sub OnRename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)
If StorageCaching Then
If File.Exists(StorageCache & e.OldName) Then
System.IO.File.Copy(StorageCache & e.OldName, StorageCache & e.Name)
End If
End If
End Sub