posted on Thursday, March 29, 2007 5:47 PM
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)
Public Sub SendEmail(ByVal sFromEmailAddress As String, ByVal sToEmailAddress As String, _
ByVal sEmailSubject As String, ByVal sEmailBody As String)
Dim objMail As New MailMessage
objMail.From = sFromEmailAddress
objMail.To = sToEmailAddress
objMail.Subject = sEmailSubject
objMail.Body = sEmailBody
objMail.BodyFormat = MailFormat.Text
Catch ex As Exception