Sub Main()
Try
Dim output, filename1, filename2, filename3, date1, date2 As String
'today's final
output += "Report Dates: " & date1 & " and " & date2
filename1 = "filename1.doc"
SaveToFile(output, filename1)
'today's daily
output = "Report Dates: " & date1 & " and " & date2
filename2 = "filename2.doc"
SaveToFile(output, filename2)
'yesterday's final
output = "Report Dates: " & date1 & " and " & date2
filename3 = "filename3.doc"
SaveToFile(output, filename3)
'email files here
SendEmail(to, body,date1);
'detele temp files
DeleteFile(filename1)
DeleteFile(filename2)
DeleteFile(filename3)
Catch e As Exception
cEmail.SendErrorEmail("me@hme.com", e.Message)
End Try
End Sub
Sub SaveToFile(ByVal text As String, ByVal fileName As String)
Dim path As String = "c:\temp\" & fileName
Try
If File.Exists(path) = True Then
File.Delete(path)
End If
' Create a file to write to.
Dim sw As StreamWriter = File.CreateText(path)
sw.WriteLine(text)
sw.Flush()
sw.Close()
' Open the file to read from.
Dim sr As StreamReader = File.OpenText(path)
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop
sr.Close()
Catch e As Exception
message = "in SaveToFile " & e.Message
cEmail.SendErrorEmail("me@hme.com", message)
End Try
End Sub
Sub DeleteFile(ByVal fileName As String)
Dim path As String = "c:\temp\" & fileName
Try
If File.Exists(path) = True Then
File.Delete(path)
End If
Catch e As Exception
message = "in DeleteFile " & e.Message
cEmail.SendErrorEmail("me@hme.com", message)
End Try
End Sub
i’m getting the following error:
in DeleteFile The process cannot access the file
‘c:\temp\filename2.doc’ because it is being used by another process.
am i supposed to release any processes before deleting the files? what am i missing?
EDIT: here is my “Send Email” function that sends out the files
Public Sub SendEmail(ByVal msgTo As String, ByVal msgBody As String, ByVal date1 As String)
Dim mail As New MailMessage()
Dim objSMTP As New SmtpClient()
Dim filename As String
''''''''''''''''''''''''''''''''''''''
Try
mail.To.Add(msgTo)
mail.Bcc.Add("me@hme.com")
mail.Priority = MailPriority.Normal
mail.IsBodyHtml = True
mail.Subject = "subject"
mail.Body = msgBody
filename = "filename1.doc"
Dim DOERecords As New Net.Mail.Attachment("C:\temp\" & filename) 'create the attachment
filename = "filename2.doc"
Dim FOERecords As New Net.Mail.Attachment("C:\temp\" & filename) 'create the attachment
filename = "filename3.doc"
Dim FOERecords2 As New Net.Mail.Attachment("C:\temp\" & filename) 'create the attachment
mail.Attachments.Add(DOERecords) 'add the attachment
mail.Attachments.Add(FOERecords) 'add the attachment
mail.Attachments.Add(FOERecords2) 'add the attachment
objSMTP.Send(mail)
Catch ex As Exception
Throw ex
End Try
End Sub
You need to dispose all Attachment objects, or you will leave open file handles behind.
You can simply call Dispose on the MailMessage. This will trigger the dispose on any attachments.
this could be avoided if you encapsulate the mail object with the
Usingstatement