Help with FileSystemWatcherService

K

Kerberos

Hope someone has some helpful insight...

I've searched the groups for a answer for these problems, but I have
not found anything definitive.

I've created a windows service (see code below) that monitors a
directory and processes an ASCII file using a third party command line
tool.

1.) How to configure the service / computer to watch a network drive.

The service needs to monitor a directory mapped through a VPN to a
server at a remote office. The remote office is not part of the head
office domain. The service is configured to run under its own windows
account (not localsystem), and an identical account is set up at the
other end. I can access the remote directory fine with this account,
and it has full privleges both with NTFS and share permissions. If I
configure the service to watch this folder, it gives an error of
"Invalid drive"

2.) How to determine if a file is in use before trying to delete it.

An error is sometimes generated when the service tries to delete a
file if it is still being written. Also the onCreated event fires and
starts the third party program before the file is fully written.
(takes a few seconds to copy the 100-300k file over the VPN.)

I need to wait until the file is fully written before it is processed,
and then also make sure that it is not in use before deleting it after
processing.

3.) Prevent service firing a second time if a file is written while
exsisting files are being processed.

The thirdparty program sends the ascii file to a scale through a
serial port, so having the service run again while the port is already
in use is a bad thing.

Can I do a objFSWatch.EnableRaisingEvents = False at the start of the
onChange event, and re-enable watching when processing is done?

Thank you in advance for any suggestions!

Peter


<-- Start Code -->

Imports System.ServiceProcess
Imports System.IO
Imports System.Diagnostics


Public Class FileWatch
Inherits System.ServiceProcess.ServiceBase
Private objFSWatch As FileSystemWatcher
Private strPath As String
Private strFile As String
Private strExecFile As String
Private strExecPath As String
Private strParams As String

Protected Overrides Sub OnStart(ByVal args() As String)
Dim objConfig As Configuration.ConfigurationSettings
'Set parameters from XML config file
strPath = objConfig.AppSettings("filePath")
strFile = objConfig.AppSettings("fileName")
strExecFile = objConfig.AppSettings("execFile")
strExecPath = objConfig.AppSettings("execPath")
strParams = objConfig.AppSettings("params")

'Create the FSWatcher object and set properties
objFSWatch = New System.IO.FileSystemWatcher
objFSWatch.Path = strPath
objFSWatch.NotifyFilter = IO.NotifyFilters.DirectoryName
objFSWatch.NotifyFilter = objFSWatch.NotifyFilter Or
IO.NotifyFilters.FileName
objFSWatch.NotifyFilter = objFSWatch.NotifyFilter Or
IO.NotifyFilters.Attributes

'Add the OnChange Handler
AddHandler objFSWatch.Created, AddressOf OnChange

'Start the watcher
objFSWatch.EnableRaisingEvents = True
End Sub

Protected Overrides Sub OnStop()
objFSWatch.EnableRaisingEvents = False
End Sub

Private Sub OnChange(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
Dim objEventLog As New EventLog
Dim i As Integer
Dim arrFileList(0) As String
Dim objDirInfo As New DirectoryInfo(strPath)
Dim objfile As FileInfo
Dim strExecute As String

objEventLog.Source = "FileWatch"

Try
'Build and array of any files in the watched directory
For Each objfile In objDirInfo.GetFiles
'If the filename matches the configured file to watch
for, add to array
If Left(objfile.Name, strFile.Length) = strFile Then
If i <> 0 Then
ReDim Preserve arrFileList(UBound(arrFileList)
+ 1)
End If
arrFileList(i) = objfile.Name
i = i + 1
End If
Next

For i = 0 To UBound(arrFileList)
'Build the execute command for fis3dw.exe
strExecute = strExecPath & strExecFile & strParams &
strPath & arrFileList(i)
'Execute fis3dw, wait for it to finish before
continuing
Shell(strExecute, AppWinStyle.Hide, True)
'Write a record to the event viewer
objEventLog.WriteEntry("FileWatch Log", "Scale file "
& arrFileList(i).ToString & " successfully processed on " &
CStr(TimeOfDay), EventLogEntryType.Information)
'Delete the file when it is done.
System.IO.File.Delete(strPath & arrFileList(i))
Next
Catch ex As Exception
objEventLog.WriteEntry("Filewatch Log", "An error occured:
" & ex.Message, EventLogEntryType.Error)
End Try
End Sub
End Class

<-- End Code -->
 
K

Kerberos

No one has any suggestions at all? These do not seem to be big
issues, surely there is a solution...

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top