VB.NET  •  Monitor File Changes

Listing 2. The RunCheck and RunAlarmAction routines are the core of the FileChangeMonitor Service app. RunCheck determines if a file hasn't changed in its defined interval and calls the RunAlarmAction routine to report the occurrence through the event log and by sending e-mail.

Private Sub RunCheck()
   Dim iFile As Integer
   For iFile=0 To m_MonitorControl.FileCount - 1
      With m_MonitorControl.Files(iFile)
         If .NextCheck < Now Then
            .NextCheck = NextCheckTime _
               (.NextCheck, _
               .CheckIntervalTimeSpan)
            If .CheckEnabled Then
               If Not CheckFileChanged(.Path, _
                  .LastFileDate) Then
                  RunAlarmAction _
               (      m_MonitorControl.Files( _
                     iFile))
                  .FailureCount += 1
               Else
                  .SuccessCount += 1
               End If
            End If
         End If
      End With
   Next
End Sub

Private Sub RunAlarmAction _
   (ByVal fc As MonitorFile)
   Dim sAlarmText As String
   sAlarmText = "<alarm text here>"
   'Action: 0=None,1=EventLog,2=Email,3=Both
   If fc.AlarmAction And AlarmActionConstants _
      .AlarmEventLog Then
      EventLog.WriteEntry(ServiceName & _
         " - AlarmAction", sAlarmText)
   End If
   If fc.AlarmAction And AlarmActionConstants _
      .AlarmEmail Then
      SendEmail(ServiceName, sAlarmText)
   End If
End Sub