Help -Delete uploaded file - Getting File in use error

Tech-Archive recommends: Speed Up your PC by fixing your registry



I am trying to delete an uploaded file after it has been sent as an email
attachment. This started after I installed the beta 2 release and changed
the code to use system.net.mail. I get the same error if I try to delete the
file using the explorer unless I wait for some undetermined amount of time.
Here is the button click and send mail code I am using. I have not had any
luck getting the sendAsync working. The email is never delivered and I never
get a completion event. I am probably missing something simple which makes
it really hard to find. Thanks for any help.
Protected Sub butUpload_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles butUpload.Click

litStatus.Text = ""

If FileUpload1.HasFile Then

Dim savePath As String = "C:\uploads\"

savePath += FileUpload1.FileName()

Try

FileUpload1.SaveAs(savePath)

FileUpload1.Dispose()

Catch ex As Exception

litStatus.Text = ex.Message

Finally

End Try

Dim name As String

For Each name In Directory.GetFiles("c:\uploads\")

litStatus.Text = "The name of the file is: " & name & "<br>"

Next

mySendMail(savePath)

File.Delete(savePath)

End If

End Sub

Sub mySendMail(ByVal myAttachment As String)

Dim myServer As SmtpClient = New SmtpClient("mail.mydomain.com")

Dim mm As MailMessage = New MailMessage("bill@xxxxxxxxxxxx",
"bill@xxxxxxxxxxxx", "test message", "This is the message")

mm.Attachments.Add(New Attachment(myAttachment))



Try

myServer.Send(mm)

Catch ex As SmtpException

litStatus.Text = ex.Message

End Try

End Sub


.


Quantcast