posted on Thursday, March 29, 2007 5:47 PM by Obi

How to determine time difference in VB.Net

I had to write a utility to calculate the time difference between the time an error log was updated on a file server and the current time. VS.Net exposes some nifty functionality which can be leveraged to do just that. This is a small but useful utility in a programmers day to day life. Please see the code snippet below:

Private myFilePath as String = "c:\MyApp\Error.Log"

If File.Exists(myFilePath) Then

'get the time stamp
Dim dtStartTime As DateTime = File.GetLastWriteTime(myFilePath)
Dim tsEndTime As TimeSpan = DateTime.Today.Now.Subtract(dtStartTime)

If tsEndTime.TotalMinutes >= 15 Then

 'Send an Email out to IT Support
 Dim sRecipient As String = "myname@myDomain.com;her@herDomain.com"
 Dim sMessageBody As String = "Log has not been updated in more than 15 minutes"

 SendEmail("xyz@myBusiness.com", sRecipient, "Error with my Program", sMessageBody)
End If

End If

Public Sub SendEmail(ByVal sFromEmailAddress As String, ByVal sToEmailAddress As String, _
                                 ByVal sEmailSubject As String, ByVal sEmailBody As String)

      Dim objMail As New MailMessage

      Try
         SmtpMail.SmtpServer.Insert(0, "MY_EXCHANGE_SERVER")

         objMail.From = sFromEmailAddress
         objMail.To = sToEmailAddress
         objMail.Subject = sEmailSubject
         objMail.Body = sEmailBody
         objMail.BodyFormat = MailFormat.Text

         objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "0")
         SmtpMail.Send(objMail)

      Catch ex As Exception
         Throw ex.InnerException
      End Try

   End Sub

Cheers,

Obi Oberoi

 


 

Comments