Help required !!
I am new to .net and need some help developing a VB service to run every 1sec, it needs to monitor the activity of a folder on the local machine. My code is below.
So far i have got it to monitor the folder, but when a file is created i can see it has logged the event BUT there appears to be about 30 logs for the same event? any ideas.
Also i would like to check for file being created, if it = "file" then execute a batch file on the system. Is this possible? any info would be greatly appreciated.
Karl
Public folderToWatch As fileSystemWatcher
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Timer1.Enabled = True
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
Timer1.Enabled = False
End Sub
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
' create a new event log
Dim MyLog As New EventLog
' Check if the the Event Log Exists
If Not MyLog.SourceExists("WSCheckFiles") Then
' Create Log
MyLog.CreateEventSource("WSCheckFiles", "WSCheckFiles Log")
End If
folderToWatch = New FileSystemWatcher
folderToWatch.Path = "C:\abta\tmp\"
With folderToWatch
.NotifyFilter = .NotifyFilter Or NotifyFilters.FileName
.NotifyFilter = .NotifyFilter Or NotifyFilters.Attributes
'To watch sub directories....
.IncludeSubdirectories = False
End With
AddHandler folderToWatch.Created, AddressOf newLog
AddHandler folderToWatch.Deleted, AddressOf newLog
folderToWatch.EnableRaisingEvents = True
'MyLog.Source = "WSCheckFiles"
'' Write to the Log
'MyLog.WriteEntry("WSCheckFiles Log", "This is log on " & CStr(TimeOfDay), EventLogEntryType.Information)
End Sub
Private Sub newLog(ByVal Source As Object, ByVal evt As FileSystemEventArgs)
If evt.ChangeType = WatcherChangeTypes.Created Then
writeToLog(evt.FullPath, "Created")
ElseIf evt.ChangeType = WatcherChangeTypes.Deleted Then
writeToLog(evt.FullPath, "Deleted")
End If
End Sub
Private Sub writeToLog(ByVal filePath As String, ByVal fileManip As String)
Dim evtLog As New EventLog
If Not evtLog.SourceExists("WSCheckFiles") Then
evtLog.CreateEventSource("WSCheckFiles", "Log of WSCheckFiles")
End If
evtLog.Source = "WSCheckFiles"
evtLog.WriteEntry("WSCheckFiles", "File " & filePath & " has " & fileManip & " by " & Environment.UserName.ToString, EventLogEntryType.Information)
End Sub
Andrew WoosterPosted Jul 22, 2005, 9:53 AM
but I'm assuming it's a small interval). So, every time the interval passes,
you handle the elapsed event.
During this handling of the event, you proceed to call AddHandler -which adds an event handler, regardless of the fact that there was
already one created. So, you probably have multiple handlers handling
the created event, which is why you see it logged 30 times. If you wait
long enough before you create the file, you would probably see it logged
thousands of times.
There are two ways to fix this, one is to set Timer1.AutoReset to false
(see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtimerstimerclasselapsedtopic.asp). Use this fix
if you REALLY want to keep using the timer class.
Another option is to simply have a helper function that sets up the event
log and adds the handlers to watch the folder, which you call in the OnStart
method. Don't forget to include another helper method to remove the handlers
during the onstop method.
I hope that fixes your problem.