Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Folder Sync using FileSystemWatcher

Status
Not open for further replies.

aclayborne

Programmer
May 3, 2000
49
0
0
US
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top