1 Attachment(s)
Problem using FileSystemWatcher
Hi all,
I'm using FileSystemWatcher to monitor folder for changes( new file added). This application is developed as a Windows Services.
The problem is - sometimes it cannot detect the changes that occurred on the folder (new file added).
Is there any tips that I can use to solve this problem...PLEASE :( ?
Below are the codes from my application:
declarations:
Code:
#Region " Private Variables "
Private m_objLog As EventLog
Private m_objWatcher As IO.FileSystemWatcher
Dim gFTPDirectory As String
Dim gFTPFilePrefix As String
Dim gQTFSPath As String
Dim gobjTProcessStart As New System.Threading.ThreadStart(AddressOf ProcessFilesAtStartUp)
Dim gobjTProcess As New System.Threading.Thread(gobjTProcessStart)
#End Region
When service is started:
Code:
Protected Overrides Sub OnStart(ByVal args() As String)
Try
'initialize new event log
m_objLog = New EventLog()
'check if log already exists
If Not m_objLog.SourceExists("ExaQFile2QTFS") Then
m_objLog.CreateEventSource("ExaQFile2QTFS", "Application")
End If
m_objLog.Source = "ExaQFile2QTFS"
'read the setting and get the tag list
ReadINIFile()
'initialize Watcher
m_objWatcher = New IO.FileSystemWatcher()
'Set Path to Watch
m_objWatcher.Path = gFTPDirectory
'Set Filter to *.*
m_objWatcher.Filter = gFTPFilePrefix & "*.txt"
'Watch Subdirectories
m_objWatcher.IncludeSubdirectories = False
'Set Changes to be watched
m_objWatcher.NotifyFilter = IO.NotifyFilters.FileName
'Pointer on Sub for events
AddHandler m_objWatcher.Created, AddressOf Changed
'Start Watching
m_objWatcher.EnableRaisingEvents = True
'prepare for system start up...thread
gobjTProcess.IsBackground = True
gobjTProcess.Start()
Catch e As Exception
m_objLog.WriteEntry("[ Message= " & e.Message & ":: Source= " & e.Source & " ] at OnStart", EventLogEntryType.Error)
Finally
End Try
End Sub
code when folder changed:
Code:
#Region " Subs for Delegate "
Private Sub Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
Try
ProcessFiles()
Catch ex As Exception
m_objLog.WriteEntry("[ Message= " & ex.Message & ":: Source= " & ex.Source & " ] at Changed", EventLogEntryType.Error)
Finally
End Try
End Sub
#End Region
when service is stoped:
Code:
Protected Overrides Sub OnStop()
Try
m_objLog.Dispose()
m_objWatcher.Dispose()
Catch e As Exception
m_objLog.WriteEntry("[ Message= " & e.Message & ":: Source= " & e.Source & " ] at OnStop", EventLogEntryType.Error)
End Try
End Sub
Please feel free to see my code.......:)
Thank you very very much in advance for the kindness help and tips.
Regards.
Re: Problem using FileSystemWatcher
We have the same issue in our project. We are watching for new files using the filesystemwatcher however the file can take up to 1 minute to finish writing. The service does not process these files as a result. We have had to resort to writing the files (PDF) to a staging folder and then moving them into the watched folder. This is not ideal - we would like to eliminate this workaround. Any suggestions ?
Re: Problem using FileSystemWatcher
Re: Problem using FileSystemWatcher
I believe you'll find that if you create a FileInfo object and test the Attributes property. If it includes the Offline attribute then the file has not finished being created and you should not try to open it. You can use a loop and repeatedly test until the Offline attribute is not found. I'm not 100% sure but I think this will work, and if so it will avoid throwing and handling exceptions, which should always be avoided if possible.