1
Reply

Creating a VB Service to monitor folder activity

karl.thursfield

karl.thursfield

Jul 21 2005 4:26 AM
3.5k

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


Answers (1)