111 lines
3.7 KiB
VB.net
111 lines
3.7 KiB
VB.net
Imports System.IO
|
|
Imports DigitalData.Modules.Logging
|
|
|
|
Public Class FileWatcher
|
|
Private ReadOnly _Logger As Logger
|
|
Private ReadOnly _Watchers As List(Of FileSystemWatcher)
|
|
Private ReadOnly _Files As Dictionary(Of String, FileProperties)
|
|
|
|
Private _Path As String
|
|
|
|
Public OfficeFilters As New List(Of String) From {"docx", "xlsx", "pptx"}
|
|
|
|
Public Sub New(LogConfig As LogConfig, Path As String)
|
|
_Logger = LogConfig.GetLogger()
|
|
_Files = New Dictionary(Of String, FileProperties)
|
|
_Watchers = New List(Of FileSystemWatcher)
|
|
_Path = Path
|
|
|
|
For Each oFilePath As String In Directory.EnumerateFiles(_Path)
|
|
Try
|
|
If IO.File.Exists(oFilePath) Then
|
|
_Files.Add(oFilePath, New FileProperties With {
|
|
.CreatedAt = DateTime.Now,
|
|
.ChangedAt = Nothing
|
|
})
|
|
End If
|
|
Catch ex As Exception
|
|
_Logger.Error(ex)
|
|
_Logger.Warn("File {0} cannot be watched!")
|
|
End Try
|
|
Next
|
|
End Sub
|
|
|
|
Public Sub Add(Filter As String, Optional SpecialTreatmentFilters As List(Of String) = Nothing)
|
|
_Watchers.Add(CreateWatcher(Filter))
|
|
End Sub
|
|
|
|
Public Sub Add(Filters As List(Of String), Optional SpecialTreatmentFilters As List(Of String) = Nothing)
|
|
For Each oFilter In Filters
|
|
_Watchers.Add(CreateWatcher(oFilter))
|
|
Next
|
|
End Sub
|
|
|
|
Private Function CreateWatcher(Filter As String)
|
|
Dim oWatcher = New FileSystemWatcher() With {
|
|
.Path = _Path,
|
|
.Filter = Filter,
|
|
.NotifyFilter = NotifyFilters.CreationTime _
|
|
Or NotifyFilters.LastAccess _
|
|
Or NotifyFilters.LastWrite _
|
|
Or NotifyFilters.Size _
|
|
Or NotifyFilters.FileName _
|
|
Or NotifyFilters.Attributes
|
|
}
|
|
|
|
AddHandler oWatcher.Created, AddressOf HandleFileCreated
|
|
AddHandler oWatcher.Changed, AddressOf HandleFileChanged
|
|
AddHandler oWatcher.Deleted, AddressOf HandleFileDeleted
|
|
AddHandler oWatcher.Renamed, AddressOf HandleFileRenamed
|
|
|
|
Return oWatcher
|
|
End Function
|
|
|
|
Private Sub HandleFileCreated(sender As Object, e As FileSystemEventArgs)
|
|
_Files.Add(e.FullPath, New FileProperties())
|
|
_Logger.Info("[Created] " & e.FullPath)
|
|
End Sub
|
|
Private Sub HandleFileChanged(sender As Object, e As FileSystemEventArgs)
|
|
_Files.Item(e.FullPath).ChangedAt = DateTime.Now
|
|
_Logger.Info("[Changed] " & e.FullPath)
|
|
End Sub
|
|
Private Sub HandleFileDeleted(sender As Object, e As FileSystemEventArgs)
|
|
_Files.Remove(e.FullPath)
|
|
_Logger.Info("[Removed] " & e.FullPath)
|
|
End Sub
|
|
|
|
Private Sub HandleFileRenamed(sender As Object, e As RenamedEventArgs)
|
|
Dim oProperties = _Files.Item(e.OldFullPath)
|
|
_Files.Remove(e.OldFullPath)
|
|
_Files.Add(e.FullPath, oProperties)
|
|
' Soll eine umbenannte datei als NEU gelten?
|
|
|
|
_Logger.Info("[Renamed] {0} --> {1}", e.OldFullPath, e.FullPath)
|
|
End Sub
|
|
|
|
Public Sub Start()
|
|
For Each oWatcher In _Watchers
|
|
oWatcher.EnableRaisingEvents = True
|
|
Next
|
|
End Sub
|
|
|
|
Public Sub [Stop]()
|
|
For Each oWatcher In _Watchers
|
|
If Not IsNothing(oWatcher) Then
|
|
oWatcher.EnableRaisingEvents = False
|
|
oWatcher.Dispose()
|
|
End If
|
|
Next
|
|
End Sub
|
|
|
|
Public Class FileProperties
|
|
Public CreatedAt As DateTime
|
|
Public ChangedAt As DateTime
|
|
|
|
Public Sub New()
|
|
CreatedAt = DateTime.Now
|
|
ChangedAt = Nothing
|
|
End Sub
|
|
End Class
|
|
End Class
|